BroadcastReceiver内部类不会触发

时间:2017-05-02 16:30:12

标签: java android broadcastreceiver inner-classes

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

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

WatchDog类:

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类:

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

3 个答案:

答案 0 :(得分:0)

接收者是否在清单中注册?它需要是。

e.g。

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

答案 1 :(得分:0)

如果你想要动态注册/取消注册,那么在onResume上注册并在onPause()上取消注册。如果你想静态地执行它,在manifest上注册它并使你的嵌套类静态,所以manifest可以实例化它。

答案 2 :(得分:0)

您需要至少拨打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