Java 为什么';t调用onDestroy()时服务停止
我有一个ArrayList,它应该在包含0个以上对象时启动报警服务,在包含0个对象时停止报警服务Java 为什么';t调用onDestroy()时服务停止,java,android,service,notifications,Java,Android,Service,Notifications,我有一个ArrayList,它应该在包含0个以上对象时启动报警服务,在包含0个对象时停止报警服务 public static void addPendingContact(Contact contact) { contactList.add(contact); if (1 == contactList.size()) { //Start the alarm only once. Intent intent = new Intent(myContext, Alar
public static void addPendingContact(Contact contact) {
contactList.add(contact);
if (1 == contactList.size()) { //Start the alarm only once.
Intent intent = new Intent(myContext, AlarmService.class);
myContext.startService(intent);
}
}
public static void completePendingContact(Contact contact) {
contactList.remove(contact);
Toast.makeText(myContext, contactList.size() + "", Toast.LENGTH_LONG).show();
if (0 == contactList.size()) {
Intent intent = new Intent(myContext, AlarmService.class);
myContext.stopService(intent);
Toast.makeText(myContext, "Reminder removed", Toast.LENGTH_LONG).show();
}
}
这是报警服务类
public class AlarmService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent myIntent = new Intent(this, NotificationBarAlarm.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pIntent = PendingIntent.getBroadcast(
getApplicationContext(), 0, myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
am.setInexactRepeating(AlarmManager.RTC_WAKEUP,
sp.getLong("notifications_time", System.currentTimeMillis()),
60 *1000, pIntent);
Date dd = new Date(sp.getLong("notifications_time", System.currentTimeMillis()));
DateFormat formatter = new SimpleDateFormat("HH:mm:ss:SSS");
String dateFormatted = formatter.format(dd);
Toast.makeText(getApplicationContext(), "My Service started, to ring at " + dateFormatted ,
Toast.LENGTH_LONG).show();
Log.d("Service Message", "The service should be created.");
return START_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(getApplicationContext(), "My Service stopped",
Toast.LENGTH_LONG).show();
Log.d("Service Message", "The service should be stopped.");
}
这是on Receive方法广播接收器类
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Re", Toast.LENGTH_LONG).show();
notifyManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context).setSmallIcon(android.R.drawable.ic_dialog_email)
.setContentTitle("Review Pending Contacts")
.setContentText("You have pending contacts to review.")
.setContentIntent(contentIntent)
.setAutoCancel(true);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
mBuilder.setSound(Uri.parse(sp.getString("notifications_new_message_ringtone", null)));
Log.d("Notification Service", "Notification Created");
notifyManager.notify(1, mBuilder.build());
}
问题是,即使在调用报警服务的onDestroy方法之后,通知也不会停止
我错过什么了吗
谢谢
所以在我的onDestroy()中,我必须取消警报
对<代码>报警管理器独立于您的服务。如果要停止触发服务的
AlarmManager
,则需要cancel()
AlarmManager事件。您正在使用AlarmManager启动特定间隔的服务。即使停止服务,也会在间隔达到时再次启动。因此,在我的onDestroy()中我必须取消闹钟吗?