3

IntentService でバックグラウンド タスクを実行します。(NotificationManager を使用して) 通知バーに進行状況を表示し、誰かがその通知をクリックした場合にタスクをキャンセルしたいと考えています。

ブロードキャストサービスで正常に実装しました。しかし、localBroadcast でも同じことができるのではないかと考えていました。

「registerReceiver」を「LocalBroadcastManager.getInstance(this).registerReceiver」に変更すると、コードが機能しません。保留中の意図で localbroadcast を使用できない理由を教えてください。

ありがとう、

現在、私のコードは次のとおりです。

boolean isCancelled;
Context context;

NotificationManager mNotifyManager;
NotificationCompat.Builder mBuilder;
int mId = 2;
protected void onHandleIntent(Intent intent) {
    isCancelled = false;
    context = getApplicationContext();
    IntentFilter filter = new IntentFilter();
    filter.addAction("com.migrationdesk.mylibman.MyServiceClass.STOP");
    registerReceiver(receiver, filter);
    getNotification();
    mainTask(); // THIS IS SOME BACKGROUND TASK
}

private final BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
                if(action.equals("com.migrationdesk.mylibman.MyServiceClass.STOP")){
                    isCancelled = true;
                }

    }
};

protected void getNotification() {
    Intent newintent = new Intent();
    newintent.setAction("com.migrationdesk.mylibman.MyServiceClass.STOP");
    PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, newintent, 0);
    mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mBuilder = new NotificationCompat.Builder(context);
    mBuilder.setContentTitle("Cover Download")
                .setContentText("In progress")
                .setContentIntent(pIntent)
                .setSmallIcon(R.drawable.ic_launcher);
}
4

1 に答える 1

8

しかし、localBroadcast でも同じことができるのではないかと考えていました。

それは不可能です。APendingIntentは通常のブロードキャストでのみ機能し、通知バーを管理するコードはプロセスに含まれていません。

于 2014-01-03T16:26:52.010 に答える