Posts Tagged ‘detect’

Detect if an application is installed or not

January 3, 2013 Leave a comment

There will be a situation where your applications require a pre-installed application in order to use their libraries or functions, for example, sharing on Facebook or Twitter, watch video on Youtube, scanning barcode from ZXing, launch casts on Sopcast

What need to implement is to detect whether those required applications are installed or not, if not, redirect users to market and get it installed.

1. First, check if the required app is installed.

There are two ways:

+) List all installed apps and find if it is installed. (refer: Retrieve list of installed applications)

+) Fire the intent of the required app to see if it runs. (refer: Launch an activity)

Well, the first way seems to be the classical one, in case lots of apps installed on device, it might reduce performance.

The second way looks better, and if it throws an exception if app not installed,  NameNotFoundException, it is the proper time to trigger the market to install.

2. Implementation on firing Intent.

This is how I work it out:


import android.content.Context;
import android.content.Intent;

public class Utils {

    /** handle the required pre-installed app */
    public static boolean handleRequiredApp(Context context, PackageManager pm, String packageName)
        Intent iApp = pm.getLaunchIntentForPackage(packageName);
        try {
            // this line will trigger exception if not found
            pm.getApplicationInfo(packageName, 0);
            return true;
        } catch (Exception ex) {
            // launch the market for installation
            launchMarket(context, packageName);
            return false;

    /** launch market to certain app */
    public static void launchMarket(Context context, String packageName)
        Uri uri = Uri.parse("market://details?id=" + packageName);
        Intent iDown = new Intent(Intent.ACTION_VIEW, uri);

Note that: the Exception will fire either when application is not installed (NameNotFoundException) or installed but having no real launching Acitivity (ActivityNotFoundException). 

This is the sample project if you cannot run the code: Demo code – Droid 10


Pete Houston

Categories: Tricks & Tips Tags: ,

Detect when mobile is booted completely

Some applications are designed to run or do something automatically right after the phone is booted, like services…

This is what I use to run services in my applications after boot

public class BootReceiver extends BroadcastReceiver {
    Context mContext;
    private final String BOOT_ACTION = "android.intent.action.BOOT_COMPLETED";

    public void onReceive(Context context, Intent intent) {
        mContext = context;
        String action = intent.getAction();
        if (action.equalsIgnoreCase(BOOT_ACTION)) {
            // do what you need to do here

Pete Houston

Categories: Tricks & Tips Tags: , , ,

Detect layout type (landscape/portrait)

July 13, 2011 4 comments

There will be a time you need to define your application GUI both in landscape and portrait (just rotate the phone).

Luckily, every droid has the built-in function to detect layout type. What you need to implement is:

1. Create two main.xml files for layout in both case: landscape and portrait.

2. Put those two main.xml in resources according to its type:



Just build and try your application GUI in runtime.


In case, you want to detect the timing when layout type change and you want to do some additional works in coding, you might wanna follow these steps:

1. Add android:configChanges=”orientation” to AndroidManifest.xml

2. Detect the orientation change:

public void onConfigurationChanged(Configuration newConfig) {
      Configuration c = getResources().getConfiguration();

      if(c.orientation == Configuration.ORIENTATION_PORTRAIT ) {
        // portrait

      } else if(c.orientation == Configuration.ORIENTATION_LANDSCAPE ){
        // landscape


Hope you enjoy it!


Pete Houston

Detect XY-coordinates when clicking or touching on screen

June 13, 2011 7 comments

If you want to get the XY-coordinates when users click on screen, the implementation of OnClickListener won’t do it. However, you need to override the onTouchEvent(), this will do the trick.


import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;

public class MainActivity extends Activity  {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

	public boolean onTouchEvent(MotionEvent event) {
		// MotionEvent object holds X-Y values
		if(event.getAction() == MotionEvent.ACTION_DOWN) {
			String text = "You click at x = " + event.getX() + " and y = " + event.getY();
			Toast.makeText(this, text, Toast.LENGTH_LONG).show();

		return super.onTouchEvent(event);

Hope you enjoy it!

Pete Houston