操作\u从android关闭广播接收器根本不工作
我试图在安卓设备开机和关机时发出嘟嘟声。 接收器通电工作正常,但关机不正常。 我让Broadcastreceiver检测到Intent.BOOT\u已完成,Intent.ACTION\u已关闭,但只有ACTION\u关闭不起作用 以下是关机接收器的清单:操作\u从android关闭广播接收器根本不工作,android,Android,我试图在安卓设备开机和关机时发出嘟嘟声。 接收器通电工作正常,但关机不正常。 我让Broadcastreceiver检测到Intent.BOOT\u已完成,Intent.ACTION\u已关闭,但只有ACTION\u关闭不起作用 以下是关机接收器的清单: <receiver android:name=".AutoRun" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<receiver
android:name=".AutoRun"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.ACTION_SHUTDOWN"/>
<action android:name="android.intent.action.QUICKBOOT_POWEROFF"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
}
我不明白为什么boot_completed可以正常工作,而shutdown不能。
任何意见都会对我大有帮助。
提前感谢。请阅读广播行为
注意:如果您的应用程序目标API级别为26或更高,则您不能使用清单为未专门针对您的应用程序的隐式广播声明接收器,但少数不受该限制的隐式广播除外。在大多数情况下,您可以改用计划作业
最后,当按清单声明时,您可以完成操作启动,但操作关闭您必须在应用程序检查中设置ContextregisterReceiver。谢谢您的建议。我检查了你建议的链接。但在所有这些试验之后,broadcastreceiver似乎无法从ACTION_SHUTDOWN获得任何广播。我做了Jopintent服务,只需点击按钮并关闭电源即可。按一下按钮,它工作正常,但甚至没有检测到广播,我为它做了日志。我应该尝试另一种方法来检测关闭操作。谢谢
public class AutoRun extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ToneGenerator tone = new ToneGenerator(AudioManager.STREAM_MUSIC, ToneGenerator.MAX_VOLUME);
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
tone.startTone(ToneGenerator.TONE_DTMF_5, 50);
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
} else if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) { // "android.intent.action.ACTION_SHUTDOWN"
Log.d("AUTO RUN", "Power Off");
tone.startTone(ToneGenerator.TONE_DTMF_5, 100);
}
}