How to open another application from current application on Android? – Different Approach

Posted Posted by Ercan in Android, BLOG     Comments 1 comment
Sep
13

Opening  an application from your own application is simple and you can find the solution everywhere like this :

Intent i = new Intent(Intent.ACTION_MAIN);
i.setComponent(new ComponentName("app package name", "app launch activity's classname"));
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);

The problem here is that if you do not know the application’s launch activity’s classname, you can not open the application. Of course one can say that: “I can access this file from Package Manager – Application Info”. My answer will be : “Try and see :)”. ApplicationInfo sometimes returns null values which makes the developer crazy.

Here is my approach to this situation (getLaunchIntentForPackage) :

Intent i = new Intent(Intent.ACTION_MAIN);
PackageManager manager = getPackageManager();
i = manager.getLaunchIntentForPackage("app package name");
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);

Hope this also will help to you!

1 Comment to “How to open another application from current application on Android? – Different Approach”

  • Thank you so much! Heres the code i made from this:
    Button bClock = (Button) findViewById(R.id.bOpenClock);
    bClock.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    Intent i = new Intent(Intent.ACTION_MAIN);
    PackageManager managerclock = getPackageManager();
    i = managerclock.getLaunchIntentForPackage(“com.android.deskclock”);
    i.addCategory(Intent.CATEGORY_LAUNCHER);
    startActivity(i);
    }
    });

    One Question: Is there a way to failsafe if the application dosent exist??

Post comment

Spam Protection by WP-SpamFree