我试图以编程方式注册接收器以执行操作android.intent.action.BOOT_COMPLETED,即让我有一个名为 BootReceiver 的接收器类,它扩展了BroadCastReceiver类。
所以在我的一个活动课上,我写了这段代码,
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.intent.action.BOOT_COMPLETED");
intentFilter.addAction("android.intent.action.PACKAGE_REPLACED");
BootReceiver receiver = new BootReceiver();
getApplicationContext().registerReceiver(receiver, intentFilter);
我试图对同一广播接收器进行应用程序更新以及 boot_completed 操作。
所以我尝试的是,我通过注册上述代码来运行活动,然后重新启动设备。我没有收到任何对 BootReceiveronReceive()方法的回调。
是否可以以编程方式为 boot_completed 操作声明接收器,或者是否有必要在清单文件中声明接收器。
实际上我的要求是以编程方式声明它。
提前致谢。