单击按钮时如何隐藏通知?

时间:2018-10-28 10:07:30

标签: java android

当我单击“是”或“否”按钮时,我想隐藏我的通知。我的代码运行良好,但是当我单击一个按钮时,它会转到主屏幕。我只想隐藏通知而无需返回主屏幕。您能告诉我最简单的方法吗?感谢您的任何建议:)


NotificationReceiver类:

public class NotificationReceiver extends BroadcastReceiver {
    NotificationManager notificationManager;

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

        notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Intent repeating_intent = new Intent(context, RepeatingActivity.class);
        repeating_intent.putExtra("what", 0);
        repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, repeating_intent, PendingIntent.FLAG_UPDATE_CURRENT);

        Intent yes = new Intent(context, RepeatingActivity.class);
        yes.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        yes.putExtra("what", 1);
        PendingIntent yesIntent = PendingIntent.getActivity(context, 1, yes, PendingIntent.FLAG_UPDATE_CURRENT);

        Intent no = new Intent(context, RepeatingActivity.class);
        no.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        no.putExtra("what", 2);
        PendingIntent noIntent = PendingIntent.getActivity(context, 2, no, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.drawable.logo)
                .setContentTitle("Hello")
                .setContentText("XD")
                .setAutoCancel(true)
                .addAction(R.drawable.yes, "Yes", yesIntent)
                .addAction(R.drawable.no, "No", noIntent);

        builder.getNotification().flags |= NotificationCompat.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, builder.build());
    }
}

RepeatingActivity类:

public class RepeatingActivity extends AppCompatActivity{

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        int what = getIntent().getIntExtra("what",-1);
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.cancel(0);

        if(what==0) {
            setContentView(R.layout.repeating_activity_layout);
        }
        else if(what==1) {
            Toast.makeText(getApplicationContext(), "Hello Yes!", Toast.LENGTH_LONG).show();
            finish();
            goHome();
        }
        else if(what==2) {
            Toast.makeText(getApplicationContext(), "Hello No!", Toast.LENGTH_LONG).show();
            finish();
            goHome();
        }
    }

    public void goHome() {
        Intent startMain = new Intent(Intent.ACTION_MAIN);
        startMain.addCategory(Intent.CATEGORY_HOME);
        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(startMain);
    }
}

4 个答案:

答案 0 :(得分:0)

调用notificationManager.notify(int noficationId, Notification notification)时,您将notificationId作为参数传递。在您的情况下为0。然后在您的情况下,notificationManager.cancel(int notificationId)呼叫notificationManager.cancel(0)以取消通知。

答案 1 :(得分:0)

使用标志 Notification.FLAG_AUTO_CANCEL

Removing notification after click

答案 2 :(得分:0)

使用Notification构建NotificationCompat.Builder时,应设置此builder.setAutoCancel(true);

答案 3 :(得分:0)

根据official doc

有五种删除通知的方法,如果要通过单击按钮删除通知,可以这样做:

notificationManager.cancelAll();//place this line in click listener of a button