在我的应用中,用户使用时间戳设置闹钟以通知他们何时使用该应用。当他们设置提醒时,它将每24小时通知用户。我使用AlarmManager和Service来解决问题。当我设置时间时,我每24小时收到一次通知,但有时候我会一次又一次地随机收到通知。这是一个我无法调试的错误,我不知道问题出在哪里。
这就是我的表现:
设置提醒并取消它是我的代码: 我使用AlarmManager来设置时间。
private void setReminder(){
Calendar calendar = Calendar.getInstance();
Calendar currentCalendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT, Locale.US);
String time = sdf.format(calendar.getTime());
SavingData.setReminder(time, true);
long intendedTime = calendar.getTimeInMillis();
long currentTime = currentCalendar.getTimeInMillis();
alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
if((intendedTime >= currentTime) == false){
// This will set the alarm for the next day, if time is below intentedTime
calendar.add(Calendar.DAY_OF_MONTH, 1);
intendedTime = calendar.getTimeInMillis();
}
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, intendedTime, AlarmManager.INTERVAL_DAY, pendingIntent);
Toast.makeText(this, "Alarm Scheduled!", Toast.LENGTH_LONG).show();
}
private void cancelId() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
// Time format to String
SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT, Locale.US);
String time = sdf.format(calendar.getTime());
SavingData.setReminder(time, false);
alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
// If the alarm has been set, cancel it.
if (alarmManager!= null) {
alarmManager.cancel(pendingIntent);
Toast.makeText(this, "Cancel Alarm!", Toast.LENGTH_SHORT).show();
}
}
我使用BroadCastReceiver:
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "MyReceiver!", Toast.LENGTH_SHORT).show();
Intent service = new Intent(context, MyService.class);
context.startService(service);
}
}
以下是我的服务类。在我的服务类中,我使用通知管理器创建通知信息。
public class MyService extends Service {
private static final int notificationID = 0;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@Override
@Deprecated
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
int icon = R.drawable.icon;
String tickerText = "HELLO";
long when = System.currentTimeMillis();
String contentTitle = "How Are You";
String contentText = "Time to use the App";
// Define sound URI, the sound to be played when there's a notification
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setSmallIcon(icon)
.setTicker(tickerText)
.setWhen(when)
.setContentIntent(pendingIntent)
.setSound(soundUri)
.build();
notificationManager.notify(notificationID, notification);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
答案 0 :(得分:1)
您的服务正在重启Android系统" null"当它死亡或在其他时间意图并且你没有考虑那些。您在收到意图时随时发送通知。来自文档:
intent 提供给startService(Intent)的Intent,如给定的那样。这可能 如果服务在其进程消失后重新启动,则为null 离开了,之前它还没有返回任何东西 START_STICKY_COMPATIBILITY。
如果您检查特定于警报意图的空意图或其他内容,则问题应该消失。