FATAL EXCEPTION main when using service on android

Asked

Viewed 47 times

0

Good night !

I’m trying to generate a service that can identify when the screen of the mobile phone is erased or access, so I followed some tips to get to this point that is a simple test code, but when running it the following error is presented:

java.lang.Runtimeexception: Unable to start Activity ComponentInfo{com.herdeiros.rafah.onresumeoronsleepscreen/com.herdeiros.rafah.onresumeoronsleepscreen.MainActivity}: java.lang.Illegalargumentexception: Service Intent must be Explicit: Intent { Act=com.herdeiros.Rafah.onresumeoronsleepscreen.Aescreenonoffservice }

Below follows classes and XML codes.

Manifestxml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.herdeiros.rafah.onresumeoronsleepscreen">
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".AEScreenOnOffService">
        <intent-filter>
            <action android:name="AEScreenOnOffService" />
        </intent-filter>
    </service>
</application>

</manifest>

Mainactivity.java

public class MainActivity extends AppCompatActivity {

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

        //Start the BroadCast Service
        Intent io = new Intent();
        io.setAction("com.herdeiros.rafah.onresumeoronsleepscreen.AEScreenOnOffService");
        startService(io);

    }
    }

Aescreenonoffservice.Java

public class AEScreenOnOffService extends Service {
    BroadcastReceiver mReceiver = null;

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

        //Test
        //Toast.makeText(getBaseContext(), "Test on start service", 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 (AESERVICE)
            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(), "Screen Off", Toast.LENGTH_SHORT).show();

        } else {

            //your code here
            //Some time required to start any service
            Toast.makeText(getBaseContext(), "Screen Off", Toast.LENGTH_SHORT).show();
        }

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {

        Log.i("ScreenOnOff", "Service distroy");
        if (mReceiver != null){
            unregisterReceiver(mReceiver);
        }
    }
    }

Aescreenonoffreceiver.Java

public class AEScreenOnOffReceiver extends BroadcastReceiver {

    private boolean screenOff;

    @Override
    public void onReceive(Context context, Intent intent) {

        //Toast.makeText(context, "Toast do Receiver", Toast.LENGTH_SHORT).show();

        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
            screenOff = true;

        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
            screenOff = false;

        }

        //Toast.makeText(context, "BroadcastReceiver msg 2 "+screenOff, Toast.LENGTH_SHORT).show();

        //Send current screen ON/OFF value to service
        Intent service = new Intent(context, AEScreenOnOffService.class);
        service.putExtra("screen_state", screenOff);
        context.startService(service);

    }
    }

Thank you!

1 answer

2

You need to make explicit the application that will "solve" your Intent, which in this case is your own:

Intent io = new Intent();
io.setAction("com.herdeiros.rafah.onresumeoronsleepscreen.AEScreenOnOffService");
io.setPackage("com.herdeiros.rafah.onresumeoronsleepscreen");
startService(io);

You can also do it directly:

Intent intent = new Intent(context,AEScreenOnOffService.class);
context.startService(intent);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.