繁体   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