0

Boa noite !

Estou tentando gerar um serviço que consiga identificar quando a tela do celular está apagada ou acessa, para isso segui algumas dicas até chegar nesse ponto que é um simples codigo de teste, porém ao executa-lo o seguinte erro é apresentado:

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 }

Abaixo segue códigos das classes e XML.

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

    }
    }

Obrigado!

1 Resposta 1

2

Você precisa deixar explícito a aplicação que irá "resolver" o teu Intent, que no caso é a própia:

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

Você também pode fazer de maneira direta:

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

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .