Android: запускается ли bootCompleted TAG дважды? - PullRequest
0 голосов
/ 07 апреля 2020

Я фиксирую событие BOOT_COMPLETED после перезагрузки устройства. У меня есть BroadcastReceiver, который отправляет дополнительное сообщение «bootCompleted» в JobIntentService. Я запускаю тост, если сообщение успешно получено. Тост срабатывает, а затем исчезает из интерфейса, но примерно через 6 или 7 секунд тост срабатывает снова. Почему Toast снова запускается, если кажется, что JobIntentService снова получает TAG «bootCompleted» от BroadcastReceiver?

public class RebootReceiver extends BroadcastReceiver {

    private static final String LOCKED_BOOT_COMPLETED = "android.intent.action.LOCKED_BOOT_COMPLETED";
    private static final String ACTION_BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED";
    private static final String QUICKBOOT_POWERON = "android.intent.action.QUICKBOOT_POWERON";
    private static final String HTC_QUICKBOOT = "com.htc.intent.action.QUICKBOOT_POWERON";
    private static final String TIME_SET = "android.intent.action.TIME_SET";
    private static final String TIMEZONE_CHANGED = "android.intent.action.TIMEZONE_CHANGED";
    private static final String LOCALE_CHANGED = "android.intent.action.LOCALE_CHANGED";

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

        String action = intent.getAction();
        if (action == null) {
            return;
        }
        else {

            int SDK_INT = Build.VERSION.SDK_INT;
            if (SDK_INT >= Build.VERSION_CODES.N && (action.equals(LOCKED_BOOT_COMPLETED)||
            action.equals(TIME_SET)||action.equals(TIMEZONE_CHANGED)||action.equals(LOCALE_CHANGED))){
                intent.putExtra("TAG","bootCompleted");
            }
            else if (SDK_INT < Build.VERSION_CODES.N && (action.equals(ACTION_BOOT_COMPLETED) ||
            action.equals(QUICKBOOT_POWERON)||action.equals(HTC_QUICKBOOT)||
            action.equals(TIME_SET)||action.equals(TIMEZONE_CHANGED)||action.equals(LOCALE_CHANGED))){
                intent.putExtra("TAG","bootCompleted");
            }
        }
        // enqueue the job to the JobIntentService class
        RebootService.enqueueWork(context,intent);
    }
}

public class RebootService extends JobIntentService {

    static final int JOB_ID = 10000;

    public static void enqueueWork(Context context,Intent work) {
        enqueueWork(context,RebootService.class,JOB_ID,work);
    } 

    @Override
    protected void onHandleWork(@NonNull Intent intent) {
        Bundle extras = intent.getExtras(); 
        if (extras != null) {

        String classname = extras.getString("TAG");
        if (classname != null && classname.equals("bootCompleted")) {
            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(() -> Toast.makeText(RebootService.this, "RebootReceiver fired this message in RebootService", Toast.LENGTH_LONG).show());
...

Manifest
...
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WAKE_LOCK"
     android:maxSdkVersion="25"  />

    <receiver
        android:name=".RebootReceiver"
        android:directBootAware="true"  >
        <intent-filter>                
            <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            <action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
            <action android:name="android.intent.action.TIME_SET" />
            <action android:name="android.intent.action.TIMEZONE_CHANGED" />
            <action android:name="android.intent.action.LOCALE_CHANGED" />
        </intent-filter>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...