gpt4 book ai didi

android - builder.setAutoCancel(true) 不工作

转载 作者:行者123 更新时间:2023-11-29 14:22:39 25 4
gpt4 key购买 nike

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button = (Button) findViewById(R.id.button);
final Intent intent = new Intent(MainActivity.this, AlertActivity.class);
final PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
builder.setContentText("Click here");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle(getString(R.string.app_name));
builder.addAction(R.mipmap.ic_launcher, "Test", pendingIntent);
builder.setAutoCancel(true);
NotificationManagerCompat.from(MainActivity.this).notify(0, builder.build());
}
});
}

我还通过删除 builder.addAction() 进行了试验,但是当我点击通知时没有任何效果。在不添加操作的情况下单击通知时,我们如何引导用户进行特定 Activity ?另外,无论哪种情况;我无法关闭通知,除非我手动滑动将其删除。

最佳答案

使用 setContentIntent 应该可以解决您的问题:

.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));

完整代码:

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
builder.setContentText("Click here");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle(getString(R.string.app_name));
builder.setAutoCancel(true)
builder.addAction(R.mipmap.ic_launcher, "Test", pendingIntent);
builder.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));
NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
});

更新:

builder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;

关于android - builder.setAutoCancel(true) 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37873912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com