一、常见的隐式意图
二、当隐式意图没有匹配的app时:
三、当有一个以上的应用匹配你的意图时,你可以弹出一个Chooser对话框,让用户选择启动那个app,如:
显示效果如图:
1.打电话:
Uri number = Uri.parse("tel:5551234");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
2.看地图:
// Map point based on address
Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
// Or map point based on latitude/longitude
// Uri location = Uri.parse("geo:37.422219,-122.08364?z=14"); // z param is zoom level
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
3.浏览网页:
Uri webpage = Uri.parse("http://www.android.com");
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
二、当隐式意图没有匹配的app时:
如果你调用了一个没有app匹配的意图,你的程序就会崩溃,要解决这个问题,可以在调用意图前先查询可匹配
意图的Activity,如:
PackageManager packageManager = getPackageManager();
List activities = packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
boolean isIntentSafe = activities.size() > 0;
如果该布尔值为true,则表示至少有一个Activity匹配你的意图。
也可以调用resolveActivity();如:
// Verify that the intent will resolve to an activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(sendIntent);
}
三、当有一个以上的应用匹配你的意图时,你可以弹出一个Chooser对话框,让用户选择启动那个app,如:
Intent intent = new Intent(Intent.ACTION_SEND);
...
// Always use string resources for UI text.
// This says something like "Share this photo with"
String title = getResources().getString(R.string.chooser_title);
// Create intent to show chooser
Intent chooser = Intent.createChooser(intent, title);
// Verify the intent will resolve to at least one activity
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
显示效果如图: