繁体   English   中英

为什么在调用onDestroy()时Service不会停止

[英]Why doesn't Service stop when onDestroy() is called

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

我错过了什么吗?

谢谢。

所以在我的onDestroy()中,我必须取消警报吗?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM