通过Intent的resolveActivity方法,并对包管理器进行查询以确定是否有Activity能够启动该Intent:
// 拨打电话
// Create the implicit Intent to use to start a new Activity.
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:2463983"));
// 检查是否存在能执行该action的activity
.
PackageManager pm = getPackageManager();
ComponentName cn = intent.resolveActivity(pm);
if (cn == null) {
//如果没有,那么就去Google Play去找找看
Uri marketUri =
Uri.parse("market://search?q=pname:com.myapp.packagename");
Intent marketIntent = new
Intent(Intent.ACTION_VIEW).setData(marketUri);
//如果Google Play上有,那么就去下载一个APP来
// capable of performing the required action. Otherwise log an
if (marketIntent.resolveActivity(pm) != null)
startActivity(marketIntent);
else
Log.e(TAG, "Market client not available.");
}
else
startActivity(intent);