简体   繁体   中英

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",

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/

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM