gpt4 book ai didi

java - 为什么在调用 onDestroy() 时服务不停止

转载 作者:行者123 更新时间:2023-11-29 05:31:42 25 4
gpt4 key购买 nike

我有一个 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, 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 方法,通知也不会停止。

我错过了什么吗?

谢谢。

最佳答案

So in my onDestroy() I would have to cancel the Alarm?

是的。 AlarmManager 独立于您的服务。如果你想停止 AlarmManager 触发你的服务,你需要 cancel() AlarmManager 事件。

关于java - 为什么在调用 onDestroy() 时服务不停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20857274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com