1 目的
許多應用程序需要與其他應用程序進行交互,如獲取其他應用程序的資源、獲取聯繫人中的電話號碼等。本文的目的即
是與其他應用程序發生交互時的關鍵應用。
2 處理步驟
1)獲取資源目標
2)確定是否有解決或訪問目標支援的應用程序
3)如果有多個需要用戶選擇其中一個
4)處理
3 具體實現
// Build the intent
Uri webpage = Uri.parse("http://www.android.com");
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
// Verify it resolves
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(webIntent, 0);
boolean isIntentSafe = activities.size() > 0;
String title = "請選擇打開方式:";
// Create and start the chooser
Intent chooser = Intent.createChooser(webIntent, title);
// Start an activity if it's safe
if(isIntentSafe){
startActivity(chooser);
}else{
Toast.makeText(mContext, "沒有可打開的應用程序", Toast.LENGTH_SHORT).show();
}