简体   繁体   中英

How can I open an android application using another application with intent?

try {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("com.xxx.applicationname"));
            startActivity(intent);
        } catch(Exception e) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com")));
        }

it always enter catch scope. I don't know where I am doing wrong

Try this:

protected void launchApp(String packageName) {
    Intent mIntent = getPackageManager().getLaunchIntentForPackage(packageName);

    if (mIntent != null) {
        try {
            startActivity(mIntent);
        } catch (ActivityNotFoundException err) {
            Toast t = Toast.makeText(getApplicationContext(),
                    "Not FOund", Toast.LENGTH_SHORT);
            t.show();
        }
    }
}

This should help:

Intent intent = getActivity().getPackageManager().getLaunchIntentForPackage("com.xxx.app");

if (intent != null)
    startActivity(intent);
else
    Toast.makeText(getActivity(), "app is not installed", Toast.LENGTH_SHORT).show();

You can try that:

Intent intent = new Intent();
intent.setPackage("package**name");
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);

and also, you should add to this line in called application manifest:

<intent-filter>
...
<category android:name="android.intent.category.CATEGORY_DEFAULT" />
</intent-filter>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM