如何防止通知打开 activity 或在 Android 中单击时被删除?
How to prevent notification from opening an activity or removed when clicked in Android?
我正在开发 Android 应用程序。我是我的应用程序,我正在尝试向用户显示进程通知。但我想要的是我不希望通知打开 Activity 或在单击时删除。我正在做的是在用户单击按钮时显示通知。通知会说 "Transferring data"。接收方发出通知。
所以当用户点击一个按钮时,接收器被调用。但我不希望在用户单击通知时删除或关闭该通知。我也不想显示任何 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 已打开。
这些是我想要的:
- 我不想在单击通知时打开 activity。
- 我只想让它自己关闭。我该怎么做?
- 是否可以显示带按钮的选项对话框?
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?
您可以 "close" 使用
手动发送通知
nm.cancel(1); // if 1 is your notification id.
Instead can show option dialog with buttons?
与其这样做,不如在通知中显示按钮。
我正在开发 Android 应用程序。我是我的应用程序,我正在尝试向用户显示进程通知。但我想要的是我不希望通知打开 Activity 或在单击时删除。我正在做的是在用户单击按钮时显示通知。通知会说 "Transferring data"。接收方发出通知。
所以当用户点击一个按钮时,接收器被调用。但我不希望在用户单击通知时删除或关闭该通知。我也不想显示任何 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 已打开。
这些是我想要的:
- 我不想在单击通知时打开 activity。
- 我只想让它自己关闭。我该怎么做?
- 是否可以显示带按钮的选项对话框?
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?
您可以 "close" 使用
手动发送通知nm.cancel(1); // if 1 is your notification id.
Instead can show option dialog with buttons?
与其这样做,不如在通知中显示按钮。