BroadcastReceiver inner class 不触发

BroadcastReceiver inner class not trigger

我正在尝试使用内部 class 广播接收器制作看门狗功能。在父 class 上,我在构造函数中注册了广播接收器,但是 onReceive class 永远不会触发。

我该如何解决这个问题?谢谢。

看门狗class:

public class WatchDog {
private final String TAG = getClass().getSimpleName();
private static final String RECEIVER_WATCHDOG = "com.example.liviu.test_broadcastreceiver.receiver";
private final static String ACTION_WATCHDOG = "com.example.liviu.test_broadcastreceiver.action.watchdog";
private final static int ID_WATCHDOG = 40856342;
private final static int mTimeout = 1000;

public WatchDog (Context context) {
    Log.d(TAG, ":::::::::::: Watchdog CONSTRUCTOR");
    IntentFilter intentFilter = new IntentFilter(RECEIVER_WATCHDOG);
    BroadcastReceiver receiver = new WatchDogReceiver();
    context.registerReceiver(receiver, intentFilter);
    setWatchdogTimer(context, true, mTimeout);
}

final class WatchDogReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, ":::::::::::: Watchdog received - ");
        setWatchdogTimer(context, true, mTimeout);
    }
}

public void setWatchdogTimer(Context context, boolean set, int timeout) {
    Log.d(TAG, ":::::::::::: Watchdog START");
    Intent intent = new Intent(context, WatchDogReceiver.class);
    intent.setAction(ACTION_WATCHDOG);
    PendingIntent alarmIntent = PendingIntent.getBroadcast(context, ID_WATCHDOG, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (set) {
        alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + timeout, alarmIntent);
    } else {
        alarmManager.cancel(alarmIntent);
    }
}

}

和 MainActivity class:

public class MainActivity extends AppCompatActivity {
private final String TAG = getClass().getSimpleName();

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

    Log.d(TAG, ">>>>>>>>>>>> Activity START");
    new WatchDog(getApplicationContext());
}

}

Logcat 仅显示:

05-02 18:11:10.197 20415-20415/com.example.liviu.test_broadcastreceiver D/MainActivity: >>>>>>>>>>>> Activity START
05-02 18:11:10.197 20415-20415/com.example.liviu.test_broadcastreceiver D/WatchDog: :::::::::::: Watchdog CONSTRUCTOR

收件人是否在清单中注册?必须如此。

例如

<receiver
android:name=".receivers.WatchDog$WatchDogReceiver"/>

如果您想要动态 register/unregister 则在 onResume 上注册并在 onPause() 上注销。如果你想静态地做,在清单上注册它并使你的嵌套 class 静态所以清单可以实例化它。

您需要至少拨打一次 setWatchdogTimer() 才能设置闹钟。

public class MainActivity extends AppCompatActivity {
private final String TAG = getClass().getSimpleName();

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

    Log.d(TAG, ">>>>>>>>>>>> Activity START");
    WatchDog watchDog = new WatchDog(getApplicationContext());
    watchdog.setWatchdogTimer(this, true, 1000);
}

如果 BroadcastReceiver 对您的应用来说是本地的,也请考虑使用 LocalBroadcastReceiver