如何防止在Android中单击时打开活动或删除通知?
我正在开发一个Android应用程序。我正在尝试向用户显示进程通知。但我想要的是,我不希望在单击某个活动时打开该活动或将其删除。我所做的是当用户单击按钮时显示通知。通知将显示“传输数据”。通知从接收方发出 所以,当用户单击一个按钮时,将调用接收器。但我不希望在用户单击时删除或关闭该通知。我也不想展示任何活动。我只想在数据处理完成后自动关闭 这是我的接收器,单击按钮时显示通知如何防止在Android中单击时打开活动或删除通知?,android,notifications,android-notifications,android-notification-bar,Android,Notifications,Android Notifications,Android Notification Bar,我正在开发一个Android应用程序。我正在尝试向用户显示进程通知。但我想要的是,我不希望在单击某个活动时打开该活动或将其删除。我所做的是当用户单击按钮时显示通知。通知将显示“传输数据”。通知从接收方发出 所以,当用户单击一个按钮时,将调用接收器。但我不希望在用户单击时删除或关闭该通知。我也不想展示任何活动。我只想在数据处理完成后自动关闭 这是我的接收器,单击按钮时显示通知 public class GalleryImageUploadReceiver extends BroadcastRece
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);
}
}
在按钮单击事件中调用Receiver,如下所示
GalleryImageUploadReceiver receiver = new GalleryImageUploadReceiver();
receiver.uploadImages(getBaseContext(),new Intent());
正如你所看到的,我将“正在进行”设置为“真”,因为我想使其不可破坏。但问题是它是关闭的,因为某个活动已打开
这就是我想要的:
notification.setAutoCancel(true);
我不想在单击通知时打开活动
去掉这个
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);
我建议你仔细阅读通知文件。这是非常简单的东西
如果你愿意,我怎么才能关门 您可以使用手动“关闭”通知
nm.cancel(1); // if 1 is your notification id.
而是可以用按钮显示选项对话框
不要这样做,只需在通知中显示按钮。不要调用setContentIntent方法。要不解除onClick,请使用setAutoCancel(false)Hi@Tim我有一个问题,即setAutoCancel(boolean autoCancel)是否用于在用户触摸通知时自动解除通知()。所以,如果用户选择toches,那么它是否会激发意图?@roman_empire只有在您使用
setContentIntent
指示它这样做时,它才会激发意图。这与设置自动取消无关,我现在明白了,谢谢你,如果我愿意,我如何关闭?此外,我添加了问题3。请也给我一个答案。谢谢你的支持。