使用服务启动可以采取操作的持续通知

时间:2016-11-12 00:13:31

标签: android android-intent android-broadcastreceiver

我已经创建了一项服务,以便我可以持续通知。我希望这个通知有一个选项,可以将意图发送给我的另一个活动。

该选项将固有地停止应用程序生成的每个进程。因此,一旦按下操作,通知也需要关闭。

我试图使用待处理的广播意图来完成此操作,但出于某种原因,广播接收器并没有捕捉到意图。我在服务的onCreate()方法中动态注册接收器,并在服务的onDestroy()方法中取消注册。

public class myService extends IntentService {
    private boolean shown = false;
    private ButtonReceiver buttonReceiver;

    //Side note: this is legacy code. Necessary?
    public myService() {
        super("myService");
    }

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

        IntentFilter filter = new IntentFilter("com.myapp.KILL_NOTIFICATION");
        this.buttonReceiver = new ButtonReceiver();
        this.registerReceiver(this.buttonReceiver, filter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(this.buttonReceiver);
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        if(intent.getAction() == "com.myapp.START_SERVICE") {
            //generate a unique id for the notification
            int id = Integer.parseInt(new SimpleDateFormat("ddHHmmss", Locale.US).format(new Date()));

            //create a pending intent to send off when the kill action is pressed
            Intent buttonIntent = new Intent("com.myapp.KILL_NOTIFICATION");
            buttonIntent.putExtra("notificationId", id);
            PendingIntent btPendingIntent = PendingIntent.getBroadcast(this, 0, buttonIntent, 0);

            Notification.Builder builder = new Notification.Builder(getApplicationContext());
            builder.setAutoCancel(false);
            builder.setContentTitle("my app");
            //builder.setContentText("Press this notification to kill.");
            builder.setOngoing(true);
            builder.setSmallIcon(R.drawable.ic_launcher);
            //builder.setContentIntent(pendingIntent);
            builder.addAction(R.drawable.ic_clear_black_24dp, "Kill my app", btPendingIntent);
            builder.setWhen(0);
            builder.setPriority(Notification.PRIORITY_MAX);

            Notification notification = builder.build();
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            //TODO need to find first unused notif id
            notificationManager.notify(id, notification);
        }
    }

    public class ButtonReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            int notificationId = intent.getIntExtra("notificationId", 0);

            Intent killIntent = new Intent("com.myapp.KILL_EVERYTHING");
            killIntent.addCategory(Intent.CATEGORY_DEFAULT);
            startActivity(killIntent);

            //cancel notification
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.cancel(notificationId);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

IntentService方法完成后,onHandleIntent()会自动停止,因此动态注册的Receiver不会在发生广播时获取广播。

您不需要Service来维持正在进行的Notification。在您的情况下,您只需直接发出Notification,而不是启动Service来执行此操作。然后在清单中静态注册您的Receiver类,并使用显式IntentNotification广播到它。

ButtonReceiver移动到它自己的类文件中,并将其注册在清单中,如下所示:

<receiver android:name=".ButtonReceiver" />

您需要在传递到startActivity()的{​​{1}}参数上调用context,就像您onReceive()一样。您还需要将以下标志添加到getSystemService()

Intent

然后只需将killIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); buttonIntent的{​​{1}}更改为:

Notification

如果这是唯一的广播PendingIntent将会处理,您将不再需要Intent buttonIntent = new Intent(this, ButtonReceiver.class); 行动。