如何防止在Android中点击时打开活动或删除通知?

Wai*_*ein 2 notifications android android-notifications android-notification-bar

我正在开发一款Android应用.我是我的应用程序,我正在尝试向用户显示进程通知.但我想要的是我不希望通知打开一个活动或在被点击时删除.我正在做的是当用户点击按钮时我正在显示通知.通知将说"转移数据".通知从接收方发出.

因此,当用户单击按钮时,将调用接收器.但我不希望在用户单击时删除或关闭该通知.我也不想展示任何活动.我只想在数据处理完成后关闭自己.请参阅下面的方案.

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

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

在这样的按钮点击事件中调用Receiver

GalleryImageUploadReceiver receiver = new GalleryImageUploadReceiver();
        receiver.uploadImages(getBaseContext(),new Intent());
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我将持续变为真实,因为我想让它变得不可思议.但问题是因为活动被打开而关闭了.

这些是我想要的:

  1. 单击通知时我不想打开活动.
  2. 我只希望它靠近自己.那么我该如何关闭自己呢?
  3. 相反可以显示带按钮的选项对话框?

Tim*_*Tim 6

但我不希望在用户单击时删除或关闭该通知.

删除这个

notification.setAutoCancel(true);
Run Code Online (Sandbox Code Playgroud)

单击通知时我不想打开活动.

删除这个

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);
Run Code Online (Sandbox Code Playgroud)

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

如果我自己怎么能关闭?

您可以手动"关闭"通知

nm.cancel(1); // if 1 is your notification id.
Run Code Online (Sandbox Code Playgroud)

相反可以显示带按钮的选项对话框?

您应该只在通知中显示按钮,而不是这样做.