您的位置:首页 > 其它

startActivity前resolveActivity的情况

2015-08-03 09:11 417 查看
当启动第三方APP的Activity的时候,不能确定当前手机是否安装了该第三方应用程序,此时应该在startActivity前对启动的intent做一次判断,是否存在匹配的Activity供启动,示例如下(代码来自网络):

// Create the impliciy Intent to use to start a new Activity.
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:555-2368"));
// Check if an Activity exists to perform this action.
PackageManager pm = context.getPackageManager();
ComponentName cn = intent.resolveActivity(pm);
if (cn == null) {
// If there is no Activity available to perform the action
// Check to see if the Google Play Store is available.
Uri marketUri = Uri.parse("market://search?q=pname:com.myapp.packagename");
Intent marketIntent = new Intent(Intent.ACTION_VIEW).setData(marketUri);
// If the Google Play Store is available, use it to download an application
// capable of performing the required action. Otherwise log an error.
if (marketIntent.resolveActivity(pm) != null) {
context.startActivity(marketIntent);
} else {
Log.d(TAG, "Market client not available.");
}
} else{
context.startActivity(intent);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息