Android 未收到来自通知的挂起内容

Android 未收到来自通知的挂起内容,android,android-intent,android-service,android-notifications,android-intentservice,Android,Android Intent,Android Service,Android Notifications,Android Intentservice,我得到了一个IntentService,它在onHandleIntent(Intent)中同步执行一些长时间运行的工作。因此,只要服务运行,我就会显示一个通知。现在,我想在用户点击通知后停止服务 这就是我得到的。创建服务后注册的广播接收器类。当服务执行某些工作时显示的具有挂起意图的通知 但不知何故,当用户点击通知时,我从未获得广播意图。有什么想法吗 下面是代码的一部分,可以很容易地重新用于测试 public class SyncService extends IntentService

我得到了一个IntentService,它在onHandleIntent(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 cancelIntent = new Intent(this, CancelReceiver.class);

尽管将调用调用调用OnStroy()的
onReceive()
,调用调用OnStroy()的
stopSelf()
,但计划的工作将一直进行到完成。要处理此问题,请添加到同步类

private boolean cancelled = false;

onReceive
中设置为true,并在“一些长时间运行的同步工作”中定期检查
取消的
并中断。

不要使用两个通知管理器。如果用户点击通知,通知是否消失?IntentService在完成其onHandleIntent()后自动调用stopSelf()工作我想知道onDestroy()是否在您的接收器有机会广播之前取消注册。我会在每个生命周期方法中放置一些日志语句,以确认调用的顺序。当我点击通知时,服务仍在运行。当我点击它时,通知不会消失。使用logCat的好主意。我会检查的。为什么在意图的构造器中会有这种变化?你能详细解释一下吗?请给出一些反馈。你试过改变的意图吗?行吗?我不在办公室。但是一旦我测试了修改后的代码,我就会给你反馈。
Intent cancelIntent = new Intent();
private boolean cancelled = false;