My code worked in <5 but in Android 5.0 I\'m running into an issue that I don\'t quite understand.
10-23 10:18:18.945: E/AndroidRuntime(8987): java.lang.I
This works for me:
Intent intent = new Intent(ACTION);
intent.setPackage(context.getPackageName());
context.startService(intent);
If you are trying to start service, try it this way:
var intent = new Intent (this,typeof(MyBoundService));
var serviceConnection = new MyBindServiceConnection (this);
BindService (intent, serviceConnection, Bind.AutoCreate);
I used this and it works great
public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) {
//Retrieve all services that can match the given intent
PackageManager pm = context.getPackageManager();
List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);
//Make sure only one match was found
if (resolveInfo == null || resolveInfo.size() != 1) {
return null;
}
//Get component info and create ComponentName
ResolveInfo serviceInfo = resolveInfo.get(0);
String packageName = serviceInfo.serviceInfo.packageName;
String className = serviceInfo.serviceInfo.name;
ComponentName component = new ComponentName(packageName, className);
//Create a new intent. Use the old one for extras and such reuse
Intent explicitIntent = new Intent(implicitIntent);
//Set the component to be explicit
explicitIntent.setComponent(component);
return explicitIntent;
}
I'm working on a project where we want to allow users to use older devices. I came up with the same solution as the one mentioned in Marias answer, however I've added a conditional statement for the setPackage
call since it is only available in API 4 (Ice Cream Sandwich == SDK 14) and above. If developing for versions below that I reckon you don't need to include the setPackage
call.
In function com.google.android.vending.licensing.LicenseChecker.checkAccess(callback)
Intent serviceIntent = new Intent(new String(
Base64.decode("Y29tLmFuZHJvaWQudmVuZGluZy5saWNlbnNpbmcuSUxpY2Vuc2luZ1NlcnZpY2U=")));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
serviceIntent.setPackage("com.android.vending");
}
boolean bindResult =
mContext.bindService(
serviceIntent,
this, // ServiceConnection.
Context.BIND_AUTO_CREATE);
If you want to start a service which is in another application.you can use this:
Intent serviceIntent = new Intent("action name for the service");
serviceIntent.setPackage("the PackageName for which the service in)");//the destination packageName
context.startService(serviceIntent);