滑动时关闭通知选项卡

  • 本文关键字:通知 选项 android
  • 更新时间 :
  • 英文 :


我用异步任务和通知实现了非常基本的服务演示:

public class ImageSendEmailService extends Service implements EmailCallback {
    private static final int IMAGE_SEND_EMAIL_SERVICE_ID = 100;
    @Override
    public void onCreate() {
        super.onCreate();
    }
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent.getAction().equals(ForegroundServiceTags.STARTFOREGROUND_ACTION.getValue())) {
            EmailTask emailTask = new EmailTask();
            emailTask.setCallback(this);
            emailTask.execute();
        }
        return super.onStartCommand(intent, flags, startId);
    }
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    public void onEmailSendingStarted() {}
    @Override
    public void onEmailSendingProgressUpdate(String progressMessage) {
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentTitle("Progress");
        builder.setContentText(progressMessage);
        builder.setTicker("Notification!");
        builder.setWhen(System.currentTimeMillis());
        builder.setSmallIcon(R.drawable.ic_launcher);
        Notification notification = builder.build();
        this.startForeground(IMAGE_SEND_EMAIL_SERVICE_ID, notification);
    }
    @Override
    public void onEmailSendingCompleted() {
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentTitle("Progress");
        builder.setContentText("Progress completed");
        builder.setTicker("Notification!");
        builder.setWhen(System.currentTimeMillis());
        builder.setSmallIcon(R.drawable.ic_launcher);
        Notification notification = builder.build();
        this.startForeground(IMAGE_SEND_EMAIL_SERVICE_ID, notification);
    }
}   
public class EmailTask extends AsyncTask<Void, Void, Void> {
    private EmailCallback callback = null;
    private int progress = 0;
    private String progressMessage = null;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected Void doInBackground(Void... params) {
        this.callback.onEmailSendingStarted();
        for (this.progress = 0; this.progress <= 10; this.progress++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            this.progressMessage = String.valueOf((int) (100 * this.progress / 10)) + " %";
            this.publishProgress();
        }
        return null;
    }
    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
        this.callback.onEmailSendingProgressUpdate(this.progressMessage);
    }
    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        this.callback.onEmailSendingCompleted();
    }
    public void setCallback(EmailCallback callback) {
        this.callback = callback;
    }
}

如您所见,我没有将通知设置为正在进行,但通知仍然不可杀死。我希望只要用户决定将通知从通知栏上滑动,通知就会保留。

我该怎么做?

顺便说一句,当用户滑动可能杀死长时间运行的进程的通知时,我也可以发出警告或其他东西吗?

我希望只要用户决定滑动,通知就会保留 它远离通知栏。

builder.setAutoCancel(false); 将使通知保留在原位,即使用户单击它也是如此。用户必须将其滑开。

builder.setOngoing(true)将使您的通知无法滑动。

不要将这两者一起使用,除非您有其他方法可以删除通知。

当用户滑动通知时,我也可以发出警告或其他东西吗,这可能会杀死 长时间运行的进程?

前台服务应使用正在进行的通知(因此用户不会通过轻扫意外终止它(,该通知将在单击时启动活动。此活动可能包含以下内容:

  • "停止"按钮
  • 服务的当前状态
  • 迄今为止的结果
  • 等。

搜索本文以获取setContentIntent,了解如何通过单击通知来启动活动。

最新更新