如何通过自身前台通知停止服务

30

我有一个正在运行的Service,在它的onStartCommand中,我正在执行startforeground以避免被系统杀死。

public int onStartCommand(Intent intent, int flags, int startId) {
    if (ACTION_STOP_SERVICE.equals(intent.getAction())) {
        Log.d(TAG,"called to cancel service");
        manager.cancel(NOTIFCATION_ID);
        stopSelf();
    }
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle("abc");
    builder.setContentText("Press below button to stoP.");
    builder.setPriority(NotificationCompat.PRIORITY_HIGH);
    builder.setSmallIcon(R.drawable.ic_launcher);

    Intent stopSelf = new Intent(this, SameService.class);
    stopSelf.setAction(this.ACTION_STOP_SERVICE);
    PendingIntent pStopSelf = PendingIntent.getService(this, 0, stopSelf,0);
    builder.addAction(R.drawable.ic_launcher, "Stop", pStopSelf);
    manager.notify(NOTIFCATION_ID, builder.build());
}

但是在按下按钮后,PendingIntent不起作用,我的activity也没有被它停止。

请问有人能告诉我,我做错了什么或有没有其他解决方案可以通过自己制作的前台notification来停止服务。

谢谢

3个回答

42
回答自己的问题,为了像我这样的其他查找者提供帮助。
问题出在下面这行代码。
 PendingIntent pStopSelf = PendingIntent.getService(this, 0, stopSelf,0);

问题的原因是末尾的数字0。 我已经使用PendingIntent.FLAG_CANCEL_CURRENT进行替换,现在它可以工作了。

更正后的代码如下:

PendingIntent pStopSelf = PendingIntent.getService(this, 0, stopSelf,PendingIntent.FLAG_CANCEL_CURRENT);

请查看FLAG_CANCEL_CURRENT或FLAG_UPDATE_CURRENT的更多解释。


1
上述想法将无法正常工作。服务应该先停止它的线程,因此有时会出现奇怪的行为。您应该在循环/计算方法中放置一些标志,并调用 "return;",然后可以通过 stopself() 停止服务或等待其自行完成。如果需要示例,我可以展示。所以请问。

6
我请求提供一个例子。 - hannes ach

1
如果您正在使用该服务,则该服务无法自行停止,它在后台运行,您需要在此代码中停止。我在通知按钮单击时停止了服务,这对我起作用了。请尝试这个。
public class AlarmSoundService extends Service {
    public static final int NOTIFICATION_ID = 1;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public int onStartCommand(final Intent intent, int flags, int startId) {
        if (intent != null) {
            if (intent.getAction().equals(Constants.ACTION_START)) {
                final Handler handler = new Handler();
                Timer timer = new Timer();
                TimerTask doAsynchronousTask = new TimerTask() {
                    @Override
                    public void run() {
                        handler.post(new Runnable() {
                            public void run() {
                                try {
                                    Date date = new Date();
                                    List<Event> list = SharedPref.getInstance(getApplicationContext()).getEvents();
                                    for (int i = 0; i < list.size(); i++) {
                                        Event a = list.get(i);
                                        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yy", Locale.getDefault());
                                        String currentDate = format.format(date);
                                        if (a.getDate().equals(currentDate)) {
                                            date = new Date();
                                            format = new SimpleDateFormat("hh:mm a", Locale.ENGLISH);
                                            if (a.getTime().equals(format.format(date))) {
                                                playAlarmNotification(a.getTitle(), a.getDescription());
                                            }
                                        }
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                    }
                };
                timer.schedule(doAsynchronousTask, 0, 1000);
            } else if (intent.getAction().equals(Constants.ACTION_STOP)) {
                stopForegroundService();
            }
        }
        return START_STICKY;
    }

    public void playAlarmNotification(String Title, String Description) {

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Intent stopnotificationIntent = new Intent(this, AlarmSoundService.class);
        stopnotificationIntent.setAction(Constants.ACTION_STOP);
        PendingIntent Intent = PendingIntent.getService(this, 0, stopnotificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id")
                .setSmallIcon(R.drawable.ic_access_time_black_24dp)
                .setContentTitle(Title)
                .setContentText(Description)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setColor(Color.BLUE)
                .setDefaults(Notification.DEFAULT_ALL)
                .setFullScreenIntent(pendingIntent, true)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .addAction(android.R.drawable.ic_media_pause, "Stop", Intent);


        Notification notification = builder.build();

        if (Build.VERSION.SDK_INT >= 26) {
            NotificationChannel channel = new NotificationChannel("channel_id", "background_service", NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription("hello");
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(channel);
        }
        startForeground(NOTIFICATION_ID, notification);
    }

    private void stopForegroundService() {

        stopForeground(true);
        stopSelf();
    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接