我了解如何在我自己的应用程序中打开另一个活动时使用intents和startActivity(),但是如何启动另一个应用程序?具体来说:
答案 0 :(得分:32)
如何查看Intent是否可用:
尝试调用Intent并处理ActivityNotFoundException
(如果不可用)
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();
}
或
Query the Package Manager看看是否提前:
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);
}
如何将参数传递给应用程序或了解其功能:
答案 1 :(得分:1)
您要找的是意图和意图过滤器。
您想了解的所有内容都在Android开发人员指南中详细说明。
http://developer.android.com/guide/topics/intents/intents-filters.html
答案 2 :(得分:0)
这对我有用:
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();
}