Android 重新启动后广播接收器未重新启动
我正试图让我的广播接收机在开机后立即运行。据我所知,广播接收器将在不重启的情况下运行,我刚刚了解到这一点,但我的问题是,我已将其设置为每晚午夜运行,我不想等到午夜再运行一次,因为这违背了目的。但我需要它在重启一次后立即运行。但它没有运行。有人能看出我做错了什么吗?我正在Galaxy S4和S6上尝试此操作,但未收到日志消息,说明它已重新启动 这是我的清单文件,正如您所看到的,这是必要的权限Android 重新启动后广播接收器未重新启动,android,broadcastreceiver,Android,Broadcastreceiver,我正试图让我的广播接收机在开机后立即运行。据我所知,广播接收器将在不重启的情况下运行,我刚刚了解到这一点,但我的问题是,我已将其设置为每晚午夜运行,我不想等到午夜再运行一次,因为这违背了目的。但我需要它在重启一次后立即运行。但它没有运行。有人能看出我做错了什么吗?我正在Galaxy S4和S6上尝试此操作,但未收到日志消息,说明它已重新启动 这是我的清单文件,正如您所看到的,这是必要的权限 <uses-permission android:name="android.permission.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:name=".StartActivityAtBootReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
在我的MainActivity中,我调用了另一个扩展广播接收器的类中的广播接收器
//run from boot or from button on screen
protected void runFromOutside() throws ParseException {
checkIfStartingNow();
startTheClock();
finish(); //close the app/view
}
//check if starting now pops up message to state that it is staring now
protected void checkIfStartingNow() throws ParseException {
//does some checks and displays a message popup
}
protected void startTheClock() {
// Set the alarm to run at midnight every night
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 00);
//calendar.set(Calendar.MINUTE, 01);
// Get the AlarmManager Service
mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
// Create an Intent to broadcast to the Shhh
mNotificationReceiverIntent = new Intent(MainActivity.this, Shhh.class);
// Create an PendingIntent that holds the NotificationReceiverIntent
// mNotificationReceiverPendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, mNotificationReceiverIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mNotificationReceiverPendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, mNotificationReceiverIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//Set repeating alarm that checks every minute.
mAlarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, mNotificationReceiverPendingIntent);
// set true for alarm
settings.edit().putBoolean(NotificationOn, true).apply();
Log.e("LOGS", "Entered Start the midnight alarm");
}
好吧,我想出来了,它不起作用的原因是,
在Android清单文件中不在正确的位置,所以最后我不得不将它移出应用程序
标记(放置它的正确位置),然后重新启动就起作用了。因此,请确保您的Android清单文件中有正确的XML布局,正如我所了解的,因为它可能会对您的应用程序造成严重破坏
位于应用程序
标记内,因此它从未工作过。非常重要。希望这对将来的其他人有所帮助。因此,请确保您的XML标记位于正确的位置
//run from boot or from button on screen
protected void runFromOutside() throws ParseException {
checkIfStartingNow();
startTheClock();
finish(); //close the app/view
}
//check if starting now pops up message to state that it is staring now
protected void checkIfStartingNow() throws ParseException {
//does some checks and displays a message popup
}
protected void startTheClock() {
// Set the alarm to run at midnight every night
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 00);
//calendar.set(Calendar.MINUTE, 01);
// Get the AlarmManager Service
mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
// Create an Intent to broadcast to the Shhh
mNotificationReceiverIntent = new Intent(MainActivity.this, Shhh.class);
// Create an PendingIntent that holds the NotificationReceiverIntent
// mNotificationReceiverPendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, mNotificationReceiverIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mNotificationReceiverPendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, mNotificationReceiverIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//Set repeating alarm that checks every minute.
mAlarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, mNotificationReceiverPendingIntent);
// set true for alarm
settings.edit().putBoolean(NotificationOn, true).apply();
Log.e("LOGS", "Entered Start the midnight alarm");
}