1

onHandleIntent(Intent) 内で同期的に実行時間の長い作業を行う IntentService を取得しました。そのため、サービスが実行されている限り、通知を表示しています。ユーザーが通知をタップしたら、サービスを停止したいと思います。

だからここに私が得たものがあります。サービスの作成後に登録されるブロードキャスト レシーバー クラス。サービスが何らかの作業を行うときに表示される、保留中の意図を持つ通知。

しかし、どういうわけか、ユーザーが通知をタップすると、ブロードキャストの意図が得られません。何か案が?

テスト用に簡単に再利用できるコードの一部を次に示します。

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);

このようなサービスを停止するというアイデアは、この投稿から来ています。

4

1 に答える 1

1

変化する

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

Intent cancelIntent = new Intent();

ただし、計画された作業を呼び出すどの呼び出しが呼び出さonReceive()れるかは、終了するまで続きます。これを処理するには、同期クラスに追加しますstopSelf()onDestroy()

private boolean cancelled = false;

onReceive「同期的に長時間実行される作業」でtrueに設定すると、定期的cancelledにチェックして発生します。

于 2014-07-16T20:21:16.190 に答える