Android 刷卡时关闭通知选项卡

Android 刷卡时关闭通知选项卡,android,Android,我使用异步任务和通知实现了一个非常基本的服务演示: public class ImageSendEmailService extends Service implements EmailCallback { private static final int IMAGE_SEND_EMAIL_SERVICE_ID = 100; @Override public void onCreate() { super.onCreate(); } p

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

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;
    }
}
公共类ImageSendEmailService扩展服务实现EmailCallback{
私有静态最终整数图像\发送\电子邮件\服务\ ID=100;
@凌驾
public void onCreate(){
super.onCreate();
}
公共int onStartCommand(Intent Intent、int标志、int startId){
if(intent.getAction().equals(ForegroundServiceTags.STARTFOREGROUND\u ACTION.getValue()){
EmailTask EmailTask=新建EmailTask();
emailTask.setCallback(此);
emailTask.execute();
}
返回super.onStartCommand(intent、flags、startId);
}
@凌驾
公共IBinder onBind(意向){
返回null;
}
public void onEmailSendingStarted(){}
@凌驾
public void onEmailSendingProgressUpdate(字符串progressMessage){
Notification.Builder=new Notification.Builder(此);
builder.setContentTitle(“进度”);
builder.setContentText(progressMessage);
builder.setTicker(“通知”);
builder.setWhen(System.currentTimeMillis());
builder.setSmallIcon(R.drawable.ic_启动器);
通知通知=builder.build();
此.startForeground(图像\发送\电子邮件\服务\ ID,通知);
}
@凌驾
public void onEmailSendingCompleted(){
Notification.Builder=new Notification.Builder(此);
builder.setContentTitle(“进度”);
builder.setContentText(“进度完成”);
builder.setTicker(“通知”);
builder.setWhen(System.currentTimeMillis());
builder.setSmallIcon(R.drawable.ic_启动器);
通知通知=builder.build();
此.startForeground(图像\发送\电子邮件\服务\ ID,通知);
}
}   
公共类EmailTask扩展了AsyncTask{
私有EmailCallback=null;
私有int进程=0;
私有字符串progressMessage=null;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的Void doInBackground(Void…参数){
this.callback.onEmailSendingStarted();
对于(this.progress=0;this.progress)
我希望只要用户决定刷卡,通知就会一直保留
它远离通知栏

builder.setAutoCancel(false);
将使您的通知保持在原位,即使用户单击它。用户必须将其刷走

builder.setContinuous(true)
将使您的通知不可擦除

除非您有其他方法删除通知,否则不要同时使用这两个选项

当用户刷走一个可能杀死一个用户的通知时,我是否也可以发出警告或其他什么 长时间运行的过程

前台服务应使用正在进行的通知(以便用户不会通过刷卡无意中终止通知),该通知将在单击时启动活动。此活动可能包含以下内容:

  • 停止按钮
  • 服务的当前状态
  • 迄今为止的结果
  • 等等
搜索
setContentIntent
,通过单击通知了解如何启动活动

我希望只要用户决定刷卡,通知就会一直保留 它远离通知栏

builder.setAutoCancel(false);
将使您的通知保持在原位,即使用户单击它。用户必须将其刷走

builder.setContinuous(true)
将使您的通知不可擦除

除非您有其他方法删除通知,否则不要同时使用这两个选项

当用户刷走一个可能杀死一个用户的通知时,我是否也可以发出警告或其他什么 长时间运行的过程

前台服务应使用正在进行的通知(以便用户不会通过刷卡无意中终止通知),该通知将在单击时启动活动。此活动可能包含以下内容:

  • 停止按钮
  • 服务的当前状态
  • 迄今为止的结果
  • 等等
搜索
setContentIntent
,通过单击通知了解如何启动活动

我希望只要用户决定刷卡,通知就会一直保留 它远离通知栏

builder.setAutoCancel(false);
将使您的通知保持在原位,即使用户单击它。用户必须将其刷走

builder.setContinuous(true)
将使您的通知不可擦除

除非您有其他方法删除通知,否则不要同时使用这两个选项

当用户刷走一个可能杀死一个用户的通知时,我是否也可以发出警告或其他什么 长时间运行的过程

前台服务应使用正在进行的通知(以便用户不会通过刷卡无意中终止通知),该通知将在单击时启动活动。此活动可能包含以下内容:

  • 停止按钮
  • 服务的当前状态
  • 迄今为止的结果
  • 等等
搜索
setContentIntent
,通过单击通知了解如何启动活动

我希望只要用户决定刷卡,通知就会一直保留 它远离通知栏

builder.setAutoCancel(false);
将使您的通知保持在原位,即使用户单击它。用户必须将其刷走

builder.setContinuous(true)
将使您的通知不可擦除

除非您有其他方法删除通知,否则不要同时使用这两个选项

当用户刷走一个可能杀死一个用户的通知时,我是否也可以发出警告或其他什么 长时间运行的过程

前台服务应该使用正在进行的通知(因此