0

I have a service which check for screen on of. I am calling this service from an activity which is mentioned below. Now when i call the service in any device below lollipop it works. It was working with lollipop also. Suddenly it stopped working on lollipop and gives me error java.lang.IllegalArgumentException: Service Intent must be explicit no i tried various methods and not able to understand whats happening.....

package com.androidexample.screenonoff;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class AEScreenOnOffService extends Service  {
    BroadcastReceiver mReceiver=null;

        @Override
        public void onCreate() {
            super.onCreate();

            // Toast.makeText(getBaseContext(), "Service on create", Toast.LENGTH_SHORT).show();

            // Register receiver that handles screen on and screen off logic
            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            mReceiver = new AEScreenOnOffReceiver();
            registerReceiver(mReceiver, filter);

        }

        @Override
        public void onStart(Intent intent, int startId) { 

            boolean screenOn = false;

            try{
                // Get ON/OFF values sent from receiver ( AEScreenOnOffReceiver.java ) 
                screenOn = intent.getBooleanExtra("screen_state", false);

            }catch(Exception e){}

             //  Toast.makeText(getBaseContext(), "Service on start :"+screenOn, 
                    //Toast.LENGTH_SHORT).show();

            if (!screenOn) {

                // your code here
                // Some time required to start any service
                //Toast.makeText(getBaseContext(), "Begin ", Toast.LENGTH_LONG).show();
                //  Intent yourintent = new Intent(this, postlockscreen.class);
                //yourintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                //startActivity(yourintent);


            } else {

                // your code here
                // Some time required to stop any service to save battery consumption
                //Toast.makeText(getBaseContext(), "Screen off,", Toast.LENGTH_LONG).show();
                Intent yourintent = new Intent(this, postlockscreen.class);
                yourintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(yourintent);
            }
        }

        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }

}

No when i start the service from any device lesser than lollipop it starts the service and works fine

package com.androidexample.screenonoff;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;

public class ScreenOnOff extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_on_off);

        // Start AEScreenOnOffService Service

        Intent i0 = new Intent();
        i0.setAction("com.androidexample.screenonoff.AEScreenOnOffService");
        startService(i0);
        //startService(new Intent(this, AEScreenOnOffService.class));

    }



}
  • Possible duplicate of [Android 5.0 (L) Service Intent must be explicit in Google analytics](http://stackoverflow.com/questions/26530565/android-5-0-l-service-intent-must-be-explicit-in-google-analytics) – Robert May 09 '16 at 11:11
  • uncomment the line startService(new Intent(this, AEScreenOnOffService.class)); It should work. Also comment out previous code section. – Nakul May 09 '16 at 12:20

0 Answers0