簡體   English   中英

使用隱式意圖啟動其他應用程序的服務組件

[英]Launch other app's service component using implicit intent

是否可以使用隱式意圖啟動其他應用的服務組件? 例如,如果我要觸發其他應用程序的服務,其意圖過濾器會收到“ com.example.otherService”,

Intent p = new Intent("com.example.otherService");
p.putExtra("lat", temp);
p.addCategory(Intent.CATEGORY_LAUNCHER);
p.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startService(p);

但這是行不通的。 請幫我。

好吧,您可以使用隱式意圖來調用其他應用程序的服務。 確保其他應用暴露了該意圖,並檢查其他應用是否支持該意圖。 使用它來檢查是否存在這樣的縮進。

// This snippet can obviously be wrapped in a method call for easy reuse

// Get the package manager
PackageManager packageManager = getPackageManager();
// Get activities that can handle the intent
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent,  0);
// Check if 1 or more were returned
boolean isIntentSafe = activities.size() > 0;

if (isIntentSafe) {
startActivity(intent);
}

有關更多信息,請參見鏈接http://codetheory.in/android-intents/

如果您知道確切的程序包名稱並且該服務在服務聲明中具有android:exported="true"屬性,則可以啟動其他應用程序的服務。

Intent intent=new Intent();
intent.setComponent(new ComponentName("com.xxx.yyy","com.xxx.yyy.SampleService"));
startService(intent);

使用隱式意圖

Intent intent=new Intent("ACTION_TO_START_SERVICE");
intent.setPackage("com.xxx.yyy");
startService(intent);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM