Service Intent must be explicit: Intent

I have an app some time now in which I call a service through a broadcast receiver (MyStartupIntentReceiver). The code in the broadcast receiver in order to call the service is:

public void onReceive(Context context, Intent intent) {
    Intent serviceIntent = new Intent();
    serviceIntent.setAction("com.duk3r.eortologio2.MyService");
    context.startService(serviceIntent);
}

The problem is that in Android 5.0 Lollipop I get the following error (in previous versions of Android, everything works ok):

  • SeekBar programmatically
  • Get referrer after installing app from Android Market
  • How to center layout inside of Android Dialog?
  • How do I configure android tests directory?
  • SeekBar in a NavigationDrawer
  • Cursor size limit in Android SQLiteDatabase
  • Unable to start receiver com.duk3r.eortologio2.MyStartupIntentReceiver: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.duk3r.eortologio2.MyService }
    

    What do I have to change in order for the service to be declared as explicit and start normally? Tried some answers in other similar threads but although i got rid of the message, the service wouldn’t start.

    Related posts:

    Combine iBeacon bluetooth low energy with Android 4.3
    How to resolve build path errors?
    Android Intent for HTTP MP3 stream on HTC Incredible
    how to change number picker style in android?
    Error: No toolchains found in the NDK toolchains folder for ABI with prefix: llvm
    Data Types used in SQLite
  • Frequently updating widgets (more frequently than what updatePeriodMillis allows)
  • How can i dim the background when Bottomsheet is displayed, without using Dialog?
  • Immersive mode instructions reappear every time the device enters this mode
  • Activity onStop() not called when home button is pressed in Android N multi window mode
  • Dagger example built through eclipse fails with 'Please ensure that code generation was run for this module.'
  • Android Studio 3.0 Preview Missing Imports when refactoring Kotlin code
  • 5 Solutions collect form web for “Service Intent must be explicit: Intent”

    any intent you make to a service, activity etc. in your app should always follow this format

    Intent serviceIntent = new Intent(context,MyService.class);
    context.startService(serviceIntent);
    

    or

    Intent bi = new Intent("com.android.vending.billing.InAppBillingService.BIND");
    bi.setPackage("com.android.vending");
    

    implicit intents (what you have in your code currently) are considered a security risk

    Set your packageName works.

    intent.setPackage(this.getPackageName());
    

    For lazy people like me just use

    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;
    

    }

    send context and your intent in this method and get result intent to use.

    Convert implicit intent to explicit intent then fire start service.

            Intent implicitIntent = new Intent();
            implicitIntent.setAction("com.duk3r.eortologio2.MyService");
            Context context = getApplicationContext();
            Intent explicitIntent = convertImplicitIntentToExplicitIntent(implicitIntent, context);
            if(explicitIntent != null){
                context.startActivity(explicitIntent);
                }
    
    
        public static Intent convertImplicitIntentToExplicitIntent(Intent implicitIntent, Context context) {
                PackageManager pm = context.getPackageManager();
                List<ResolveInfo> resolveInfoList = pm.queryIntentServices(implicitIntent, 0);
    
                if (resolveInfoList == null || resolveInfoList.size() != 1) {
                    return null;
                }
                ResolveInfo serviceInfo = resolveInfoList.get(0);
                ComponentName component = new ComponentName(serviceInfo.serviceInfo.packageName, serviceInfo.serviceInfo.name);
                Intent explicitIntent = new Intent(implicitIntent);
                explicitIntent.setComponent(component);
                return explicitIntent;
            }
    

    Try this. it works for me. Here MonitoringService is my service class. I have two action, which indicate service to stop or start. I send that value from my broadcast receiver depend on AIRPLANE_MODE_CHANGED.

    @Override
    public void onReceive(Context context, Intent intent) {   
        String action = intent.getAction();
    
        if(Intent.ACTION_AIRPLANE_MODE_CHANGED.equalsIgnoreCase(action)){
             boolean isOn = intent.getBooleanExtra("state", false);
             String serviceAction = isOn? MonitoringService.StopAction : MonitoringService.StartAction;
             Intent serviceIntent = new Intent(context, MonitoringService.class);
             serviceIntent.setAction(serviceAction);
             context.startService(serviceIntent);
        }
    }
    

    NOTE: I add following code to trigger my broadcast receiver named: ManageLocationListenerReceiver.

    <receiver
         android:name=".ManageLocationListenerReceiver"
         android:enabled="true"
         android:exported="true">
         <intent-filter>
             <action android:name="android.intent.action.AIRPLANE_MODE" />
         </intent-filter>
    </receiver>
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.