如果点击了操作,通知不会消失

时间:2020-04-30 09:26:51

标签: android

我有一个从流程“ com.example.notificationsender”触发的通知。使用此通知的“操作”按钮,我正在另一个进程“ com.example.notificationreceiver”中启动服务。

以下是触发通知的代码(在“ com.example.notificationsender”中):

NotificationManager mNoMan = getSystemService(NotificationManager.class);
        String text = "test test test test test test test test test test test test";
        Notification.Builder nb =
                new Notification.Builder(this, "newChannel")
                        .setSmallIcon(R.drawable.ic_launcher_background)
                        .setContentText(text)
                        .setContentTitle("Content Title")
                        .setOnlyAlertOnce(true)
                        .setStyle(new Notification.BigTextStyle().bigText(text))
                        .setVisibility(Notification.VISIBILITY_PUBLIC);

        Intent enableIntent = new Intent();
        enableIntent.setComponent(new ComponentName("com.example.notificationreceiver","com.example.notificationreceiver.MyService"));
        PendingIntent enablePendingIntent = PendingIntent.getService(this, 0, enableIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        Notification.Action action = new Notification.Action.Builder(R.drawable.ic_launcher_background, "Enable", enablePendingIntent).build();
        nb.setAutoCancel(true);
        nb.addAction(action);
        nb.setContentIntent(enablePendingIntent);
        Notification n = nb.build();
        n.flags |= Notification.FLAG_AUTO_CANCEL;
        mNoMan.notify(595, n);

而且,对于开始的服务,我拥有(在“ com.example.notificationreceiver”中):

public class MyService extends Service {
    public MyService() {
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        NotificationManager mNoMan = getSystemService(NotificationManager.class);
        mNoMan.cancel(595);
        Toast.makeText(this, "Hello World!", Toast.LENGTH_SHORT).show();
        stopSelf();
    }
}

只要服务处于同一进程中,通知似乎就可以用“ .cancel”驳回罚款。但是我需要在新流程中创建服务,而不要在以前的软件包中启动任何新组件,而只能关闭通知(与RAM相关的问题)。

是否有任何巧妙的方法可以消除“ addAction”上的通知,就像我们对内容的Action一样(例如setAutoCancel(true))?

0 个答案:

没有答案