简体   繁体   English

使用隐式意图启动其他应用程序的服务组件

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

Is it possible to launch other app's Service component using implicit intent? 是否可以使用隐式意图启动其他应用的服务组件? for example, if I want to trigger other app's service whose intent filter receives "com.example.otherService", 例如,如果我要触发其他应用程序的服务,其意图过滤器会收到“ 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);

But it doesn't work. 但这是行不通的。 Please help me. 请帮我。

Well you can use implicit intent to call other app's service. 好吧,您可以使用隐式意图来调用其他应用程序的服务。 Make sure that other app is exposing the intent and check if it is supported or not by other app. 确保其他应用暴露了该意图,并检查其他应用是否支持该意图。 Use this to check if such an indent exist. 使用它来检查是否存在这样的缩进。

// 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);
}

For more info you can see the link http://codetheory.in/android-intents/ 有关更多信息,请参见链接http://codetheory.in/android-intents/

You can start the service of the other app if you know the exact package name and that service has android:exported="true" attribute in the service declaration. 如果您知道确切的程序包名称并且该服务在服务声明中具有android:exported="true"属性,则可以启动其他应用程序的服务。

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

Using implicit 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