我想做什么
我想在特定时间从其他接收器呼叫接收器。 所以要做到这一点我使用的是alarmManager,但它无法正常工作。我已经尝试了互联网上列出的所有可能解决方案。我试着从最近4天解决这个问题,但没有运气。
@SuppressLint("NewApi")
private void setAlarmToCallNotificationService(Context context, int request_code, String notificationText, String notificationTitle) {
init(context);
Log.i("Inside notification,","Yes");
Intent intent = new Intent(context, notificationService.class);
intent.putExtra("Notification_title",notificationTitle);
intent.putExtra("Notification_text",notificationText);
//hit the notification At the 8.00 in the morning
Calendar notificationCalendar=Calendar.getInstance();
notificationCalendar.set(Calendar.HOUR_OF_DAY,12);
notificationCalendar.set(Calendar.MINUTE,51);
notificationCalendar.set(Calendar.SECOND,0);
Long time=notificationCalendar.getTimeInMillis();
System.out.println("NOTIFICATION Time is "+notificationCalendar.get(Calendar.HOUR_OF_DAY)+" "+notificationCalendar.get(Calendar.MINUTE));
Log.i("Target",time.toString());
//final int _id = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, request_code, intent, PendingIntent.FLAG_ONE_SHOT);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion < android.os.Build.VERSION_CODES.KITKAT){
alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
} else {
if (currentapiVersion < android.os.Build.VERSION_CODES.M) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent);
} else {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, pendingIntent);
}
}
}
我在updateTables和Alarm rang notificationservice类调用中设置闹钟。
清单文件:
<receiver android:name=".notificationService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".UpdateTables"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
./ adb shell dumpsys警报输出。
RTC #5: Alarm{2f5fc896 type 1 when 1484807520998 user.com.hlthee}
tag=*alarm*:user.com.hlthee/.UpdateTables
type=1 whenElapsed=+23h57m34s393ms when=2017-01-19 12:02:00
window=-1 repeatInterval=86400000 count=0
operation=PendingIntent{14ea4c17: PendingIntentRecord{3bbb9804 user.com.hlthee broadcastIntent}}
+11ms running, 0 wakeups, 1 alarms: u0a72:user.com.hlthee
*alarm*:user.com.hlthee/.UpdateTables
u0a72:user.com.hlthee +12ms running, 3 wakeups:
+11ms 0 wakes 1 alarms: *alarm*:user.com.hlthee/.UpdateTables
+1ms 3 wakes 3 alarms: *walarm*:user.com.hlthee/services.notificationService