android - 未收到通知的 PendingIntent

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

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

这就是我得到的。创建服务后注册的广播接收器类。当服务完成某些工作时显示的具有待处理 Intent 的通知。

但不知何故,当用户点击通知时,我从来没有得到广播 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);
    }
}

服务是从我的 Activity 中启动的:

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

像这样停止服务的想法来自this post .

最佳答案

改变

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

Intent cancelIntent = new Intent();

尽管 onReceive() 将被调用,它调用 stopSelf(),它又调用 onDestroy(),但计划的工作将继续进行,直到完成。要处理这个添加到同步类

private boolean cancelled = false;

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

关于android - 未收到通知的 PendingIntent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24785267/

相关文章:

android - 如何实现 Staggered GridView with Sections?

android - 如何使用 Intent 将包含可绘制对象的对象从一个 android Activity 发送到另一个 Activity ?

android - java.lang.IllegalArgumentException : Service not registered

android - 从小部件启动/停止服务

android - 卸载应用程序不会触发服务中的 onDestroy

java - if 语句中的条件始终为 true

android - 如何解决公共(public)抽象乐趣入队的参数过多(responseCallback : Callback): Unit defined in okhttp3. 调用

java - 如何制作具有通用类型和 Android Fragment 继承的接口(interface)?

java - 在 Android 中使用 Intent

android - 在 Intent 中显示特定类型的应用程序