为什么我的通知有时不会显示?
我在MainActivity中创建setRecurringAlarm(this);
,我想使用此代码每30分钟显示一次通知
这是我的代码:
private void setRecurringAlarm(Context context) {
Log.d("MyService", "setRecurringAlarm() activated...");
Calendar updateTime = Calendar.getInstance();
updateTime.setTimeZone(TimeZone.getDefault());
updateTime.set(Calendar.HOUR_OF_DAY, 12);
updateTime.set(Calendar.MINUTE, 30);
Intent downloader = new Intent(context, MyStartServiceReceiver.class);
downloader.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, downloader, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
}
这是我的MyStartServiceReceiver:
public class MyStartServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent dailyUpdater = new Intent(context, MyService.class);
context.startService(dailyUpdater);
Log.d("AlarmReceiver", "Called context.startService from AlarmReceiver.onReceive");
}
}
这是我的MyService:
public class MyService extends IntentService {
public MyService() {
super("MyServiceName");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d("MyService", "Execute MyTask");
new MyTask().execute();
this.sendNotification(this);
}
private class MyTask extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... strings) {
Log.d("MyService", "Calling doInBackground() in MyTask");
return false;
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void sendNotification(Context context) {
Log.d("MyService", "Execute sendNotification()");
// here is my notification code --> http://stackoverflow.com/questions/20558619/how-to-set-notification-to-clear-itself-on-click
}
}
答案 0 :(得分:1)
您在IntentService
中没有使用唤醒锁。由于BroadcastReciever会立即返回,因此您的手机无法保证清醒。您需要在IntentService
检查出来