AlarmManager alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE);
Intent alarmIntent=new Intent(context,UpdateTables.class);
PendingIntent pendingIntent=PendingIntent.getBroadcast(context,0,alarmIntent,PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);
Calendar alarmStartTime = Calendar.getInstance();
alarmStartTime.setTimeInMillis(System.currentTimeMillis());
//midnight
alarmStartTime.set(Calendar.HOUR_OF_DAY, 19);
alarmStartTime.set(Calendar.MINUTE, 18);
alarmStartTime.set(Calendar.SECOND, 0);
if(Calendar.getInstance().getTimeInMillis()>alarmStartTime.getTimeInMillis())
{
alarmStartTime.add(Calendar.DATE,1);
}
Log.i("TIME IS ",Long.toString(alarmStartTime.getTimeInMillis()));
System.out.println("Updating table time "+alarmStartTime);
System.out.println("Time in millseconds "+alarmStartTime.getTimeInMillis());
alarmManager.setInexactRepeating(AlarmManager.RTC,alarmStartTime.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
Log.d("Alarm","Set for midnight");
此警报正在触发其他接收者,即UpdateTables.java(这是有效的)
现在在UpdateTables.java中,它也创建了警报并将意图传递给另一个接收者。但是接收者没有收到意图。
//show notification for the test ans follow up
ArrayList<Action> notificationData=dbHelper.fetchTestAndFollowUpTime();
for (int actionitem=0;actionitem<notificationData.size();actionitem++){
Action actionItem=notificationData.get(actionitem);
long hourRemainingCompeleteTask=TimeUnit.MILLISECONDS.toHours(actionItem.getActionEndDate()-Calendar.getInstance().getTimeInMillis());
System.out.println("Action is "+actionItem.getAction_name());
System.out.println("test time is "+actionItem.getActionEndDate());
System.out.println("Current time "+Calendar.getInstance().getTimeInMillis());
System.out.println("Hour remaining "+hourRemainingCompeleteTask);
if (Long.valueOf(hourRemainingCompeleteTask).compareTo(24L)<0){
//means remaining time is less than 24 hr.
setAlarmToCallNotificationService(context,actionitem+alarmLastRequestCode+5,actionItem.getAction_name(),actionItem.getCatagory());
}else {
//in the else.
setAlarmToCallNotificationService(context,actionitem+alarmLastRequestCode+5,actionItem.getAction_name(),actionItem.getCatagory());
}
}
private static void init(Context ctx){
if (alarmManager==null)
alarmManager=(AlarmManager) ctx.getSystemService(ctx.ALARM_SERVICE);
}
@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,11);
notificationCalendar.set(Calendar.MINUTE,55);
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);
}
}
//AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
//alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
}
但问题是,updateTables.java接收器没有触发notificationService接收器
public class notificationService extends BroadcastReceiver {
public static String TAG="notificationService";
@Override
public void onReceive(Context context, Intent intent) {
String notificatioTitle=intent.getExtras().getString("Notification_title");
String notificationMsg=intent.getExtras().getString("Notification_text");
Log.i(TAG,"Notification title "+notificatioTitle);
Log.i(TAG,"Notification msg "+notificationMsg);
NotificationCompat.Builder notification=new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(notificatioTitle)
.setContentText(notificationMsg);
NotificationManager mNotificationManager =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0,notification.build());
}
}
清单文件
<receiver android:name=".UpdateTables"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".notificationService"
android:enabled="true">
输出./adb shell dumpsys alarm:
RTC #19: Alarm{3788170 type 1 when 1484142480449 user.com.hlthee}
tag=*alarm*:user.com.hlthee/.UpdateTables
type=1 whenElapsed=+23h56m29s342ms when=2017-01-11 19:18:00
window=-1 repeatInterval=86400000 count=0
operation=PendingIntent{3e5f6ce9: PendingIntentRecord{7068d6e user.com.hlthee broadcastIntent}}
u0a195:user.com.hlthee +79ms running, 3 wakeups:
+69ms 0 wakes 1 alarms: *alarm*:user.com.hlthee/.UpdateTables
+10ms 3 wakes 3 alarms: *walarm*:user.com.hlthee/services.notificationService
我根据这里给出的答案更改了代码,但仍然没有响起警报。
答案 0 :(得分:0)
要确保您的活动正在调用,请使用以下方法:
private static AlarmManager am;
private static void init(Context ctx){
if (am == null)
am = (AlarmManager) ctx.getSystemService(Activity.ALARM_SERVICE);
}
@SuppressLint("NewApi")
public static void startbyalarmWake(Context ctx, long nexttim) {
init(ctx);
Intent intent = new Intent(ctx,BRWake.class);
PendingIntent pi = PendingIntent.getBroadcast(ctx, SOMEID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion < android.os.Build.VERSION_CODES.KITKAT){
am.set(AlarmManager.RTC_WAKEUP, nexttime, pi);
} else {
if (currentapiVersion < android.os.Build.VERSION_CODES.M) {
am.setExact(AlarmManager.RTC_WAKEUP, nexttime, pi);
} else {
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, nexttime, pi);
}
}
}