I understand how to use intents and startActivity() when opening another activity within my own app, but how do you start a different app? specifically:
How to see if Intent is available:
Try calling Intent and deal with ActivityNotFoundException
if it isn't available
Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(path, "application/pdf"); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); try { startActivity(intent); } catch (ActivityNotFoundException e) { Toast.makeText(OpenPdf.this, "No Application Available to View PDF", Toast.LENGTH_SHORT).show(); }
or
Query the Package Manager to see if it is ahead of time:
PackageManager packageManager = getPackageManager(); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setType("application/pdf"); List list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); if (list.size() > 0) { intent.setDataAndType(path, "application/pdf"); startActivity(intent); }
How to pass parameters to an application or know its capabilities:
What you are looking for are intents and intent filters .
Everything you want to know is detailed on the Android developer guide.
http://developer.android.com/guide/topics/intents/intents-filters.html
This works for me:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.example.testing_app");
if (launchIntent != null) {
startActivity(launchIntent);
} else {
Toast.makeText(MainActivity.this, "testing_app is not installed", Toast.LENGTH_LONG).show();
}
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.