Android keeps intentservice running and listens to the radio

I tried to track the number of times "screen_on" was triggered without the user starting the application. The application itself shows a single activity with some charts and information. I created a small test, but I don't think this is the right way because it will drain the battery

I got the broadcast receiver "boot_completed", which started a sticky intentservice, which is using an endless loop to register the "screen_on" broadcast receiver to capture the broadcast (battery depletion problem)

Can I listen to the "screen_on" radio without service?

Jul

performance

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
    android:name=".Application"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:hardwareAccelerated="true">

    <activity
        android:name=".activities.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:enabled="true" android:name=".services.ScreenOnService" />

    <receiver android:name=".broadcast.receivers.AutoStartReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <receiver android:name=".broadcast.receivers.ScreenOnReceiver">
        <intent-filter>
            <action android:name="android.intent.action.SCREEN_ON" />
        </intent-filter>
    </receiver>
</application>

Auto start receiver

public class AutoStartReceiver extends BroadcastReceiver
{
    public void onReceive(Context aContext, Intent anIntent)
    {
        Log.i("[AutoStartReceiver]", "onReceive");
        aContext.startService(new Intent(aContext, ScreenOnService.class));
    }
}

ScreenOnReceiver

public class ScreenOnReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.i("[ScreenOnReceiver]", "onReceive");
    }
}

ScreenOnService

public class ScreenOnService extends IntentService
{
    private ScreenOnReceiver theReceiver;

    public ScreenOnService()
    {
        super(ScreenOnService.class.getName());
        theReceiver = new ScreenOnReceiver();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        Log.v("[ScreenOnService]", "onStartCommand");
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

    @Override
    protected void onHandleIntent(Intent intent)
    {
        Log.i("[ScreenOnService]", "onHandleIntent");
        registerReceiver(theReceiver, new IntentFilter(Intent.ACTION_SCREEN_ON));
        while(true);
    }

    @Override
    public void onDestroy()
    {
        Log.i("[ScreenOnService]", "onDestroy");
        unregisterReceiver(theReceiver);
        super.onDestroy();
    }
}

resolvent:

Do you have a specific reason to use intentservice instead of a normally started service?

You should be able to do this using a normally started service. Register the receiver as part of onstartcommand

like this:

public class MyService extends Service {
    private ScreenOnReceiver mReceiver;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (mReceiver == null) {
            mReceiver = new ScreenOnReceiver();
            registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_SCREEN_ON));
        }
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_STICKY;
    }

    // remember to unregister receiver in onDestroy...
}

In this way, you can avoid busy loops. Intentservice is designed as a short-term service to perform background operations. Your usage does not meet the purpose of intentservice

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>