未收到通知的待处理内容

时间:2014-07-16 15:52:33

标签: android android-intent android-service android-notifications android-intentservice

我有一个IntentService,它在onHandleIntent(Intent)中同步执行一些长时间运行的工作。所以只要服务运行,我就会显示通知。现在,我想在用户点击通知后停止服务。

所以这就是我得到的。创建服务后注册的广播接收器类。当服务执行某些操作时显示的具有pending-intent的通知。

但不知何故,当用户点击通知时,我从未获得广播意图。有什么想法吗?

以下是可以轻松重复用于测试的代码部分。

public class SyncService extends IntentService
        {

    private static final String TAG = SyncService.class.getSimpleName();
    private static final int NOTIFICATION_REF_ID = 1;

    private static final String ACTION_CANCEL = TAG + ".CANCEL";
    private CancelReceiver receiver;

    public SyncService() {
        super(SyncService.class.getSimpleName());
    }

    private class CancelReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            stopSelf();
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();

        receiver = new CancelReceiver();
        IntentFilter filter = new IntentFilter(ACTION_CANCEL);
        registerReceiver(receiver, filter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if (receiver != null)
            unregisterReceiver(receiver);
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        Intent cancelIntent = new Intent(this, CancelReceiver.class);
        cancelIntent.setAction(ACTION_CANCEL);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
                cancelIntent, 0);

        // create notification and set pending-intent
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentTitle("some caption")
                .setContentText("some hint")
                .setTicker("some caption").setSmallIcon(R.drawable.ic_action_refresh)
                .setOngoing(true).setWhen(System.currentTimeMillis())
                .setContentIntent(pendingIntent).setProgress(0, 0, true);

        Notification notification;
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
            notification = builder.getNotification();
        else
            notification = builder.build();

        // show notification
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(SyncService.class.getCanonicalName(), NOTIFICATION_REF_ID,
                notification);


        // now do some long running work synchronously


        // cancel/remove notification
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancel(SyncService.class.getCanonicalName(), NOTIFICATION_REF_ID);
    }
}

该服务从我的活动中开始:

        Intent syncIntent = new Intent(this, SyncService.class);
        startService(syncIntent);

停止此类服务的想法来自this post

1 个答案:

答案 0 :(得分:1)

更改

Intent cancelIntent = new Intent(this, CancelReceiver.class);

Intent cancelIntent = new Intent();

虽然会调用onReceive()来调用调用stopSelf()的{​​{1}},但计划的工作将一直持续到完成。要处理此问题,请添加到同步类

onDestroy()

private boolean cancelled = false; 和您的同步长时间工作中设置为true&#39;定期检查onReceive然后爆发。