gpt4 book ai didi

android - 在Android中单击时如何防止通知打开 Activity 或删除?

转载 作者:太空狗 更新时间:2023-10-29 16:13:13 25 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序。我是我的应用程序,我正在尝试向用户显示进程通知。但是我想要的是我不希望通知打开 Activity 或在单击 Activity 时将其删除。我正在做的是在用户单击按钮时显示通知。通知会说“正在传输数据”。接收者发出通知。

所以当用户点击一个按钮时,接收器被调用。但我不希望在用户单击通知时删除或关闭该通知。我也不想展示任何 Activity 。我只想在数据处理完成后自行关闭。

这是我的接收器,在单击按钮时显示通知

public class GalleryImageUploadReceiver extends BroadcastReceiver {

private Context context;
private NotificationCompat.Builder notification;

@Override
public void onReceive(Context pContext, Intent intent) {
this.context = pContext;
showProcessNotification();
doAsyncTaskInBackground()
}

public void doAsyncTaskInBackground()
{
//I want to close it here after task is finished
}

public void showProcessNotification()
{
try{

notification = new NotificationCompat.Builder(context);
notification.setAutoCancel(true);
notification.setSmallIcon(R.mipmap.ic_launcher);
notification.setWhen(System.currentTimeMillis());
notification.setTicker("Transferring data...");
notification.setContentTitle(context.getResources().getString(R.string.app_name));
notification.setContentTitle("Transferring data...");
notification.setOngoing(true);
notification.setDefaults(Notification.FLAG_ONGOING_EVENT);
Intent i = new Intent(context.getApplicationContext(), MainActivity.class);//In this line, I do not want to open any activity
i.putExtra("start", OfficeMainActivity.FRAGMENT_NOTIFICATIONS);
PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(),0,i,PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
NotificationManager nm = (NotificationManager)context.getApplicationContext().getSystemService(context.getApplicationContext().NOTIFICATION_SERVICE);
nm.notify(1,notification.build());
}
catch (Exception e)
{

}


}


public void uploadImages(Context context,Intent intent)
{
onReceive(context,intent);
}
}

像这样在按钮点击事件中调用接收器

GalleryImageUploadReceiver receiver = new GalleryImageUploadReceiver();
receiver.uploadImages(getBaseContext(),new Intent());

如您所见,我将正在进行的设置为 true,因为我想让它不可关闭。但问题是它已关闭,因为 Activity 已打开。

这些是我想要的:

  1. 我不想在点击通知时打开 Activity 。
  2. 我只希望它自己关闭。我该怎么做?
  3. 可以改为显示带按钮的选项对话框吗?

最佳答案

But I do not want that notification removed or closed when user click it.

去掉这个

notification.setAutoCancel(true);

I do not want to open an activity when notification is clicked.

删除这个

Intent i = new Intent(context.getApplicationContext(), MainActivity.class);//In this line, I do not want to open any activity
i.putExtra("start", OfficeMainActivity.FRAGMENT_NOTIFICATIONS);
PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(),0,i,PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);

我建议您仔细阅读通知文档。这是非常简单的事情。

How can I close if self please?

你可以手动“关闭”一个通知

nm.cancel(1); // if 1 is your notification id.

Instead can show option dialog with buttons?

与其这样做,不如在通知中显示按钮。

关于android - 在Android中单击时如何防止通知打开 Activity 或删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37965853/

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