我有一个ArrayList,如果它包含多于0个对象,它应该启动一个Alarm Service,并在它包含0个对象时停止它。
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.");
}
这是接收方法广播接收器类
@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方法之后,通知也不会停止。
我错过了什么吗?
感谢。
答案 0 :(得分:3)
所以在我的onDestroy()中我必须取消闹钟?
是。 AlarmManager
与您的服务无关。如果您想阻止AlarmManager
触发您的服务,则需要cancel()
AlarmManager
次事件。