IntentService正在停止每次我杀的应用 [英] IntentService is stopping everytime i kill the app

查看:114
本文介绍了IntentService正在停止每次我杀的应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于有人推荐我实现了一个IntentService做在后台的一些工作。现在我只是实现了一个非常基本的服务用一些虚拟的code至pretend一些长期运行的工作:

 公共类ImageSendEmailService扩展IntentService {
    私有静态最终诠释MY_NOTIFICATION_ID = 1;
    私人NotificationManager notificationManager = NULL;
    私人通知通知=无效;    公共ImageSendEmailService(){
        超级(EmailService);
    }    @覆盖
    公共无效的onCreate(){
        super.onCreate();        this.notificationManager =(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
    }    @覆盖
    保护无效onHandleIntent(意向意图){
        的for(int i = 0; I< = 10;我++){
            尝试{
                视频下载(1000);
            }赶上(InterruptedException的E){
                e.printStackTrace();
            }            字符串notificationText =将String.valueOf((INT)(100 * I / 10))+%;            NotificationCompat.Builder建设者=新NotificationCompat.Builder(本);
            builder.setContentTitle(进步);
            builder.setContentText(notificationText);
            builder.setTicker(通知!);
            builder.setWhen(System.currentTimeMillis的());
            builder.setDefaults(Notification.DEFAULT_SOUND);
            builder.setAutoCancel(真);
            builder.setSmallIcon(R.drawable.ic_launcher);            this.notification = builder.build();
            this.notificationManager.notify(MY_NOTIFICATION_ID,this.notification);
        }
    }
}

不幸的是停止总是当我杀的应用程序UI的过程。例如,如果进度为50%,我杀了应用程序的进展情况保持在50%,并且不会继续。该文件说,一个IntentService没有被杀,直到它的工作已经完成,但它得到我的情况下丧生。

后来IntentService应该用于若干任务:


    使用电子邮件
  1. 发送图像

  2. 在服务器上储存影像

  3. 重复任务时自动任务失败被丢失的网络连接造成的。

同样重要的是在后台运行,因为我不希望任务被打断,当用户得到一个电话。和任务的重复是更重要的。有可能是暂时没整部手机到互联网连接,低电量状态,甚至崩溃。


解决方案

  

文档说的IntentService没有被杀,直到它的工作已经完成,但它得到我的情况下丧生。


我检查文档,它只字未提这样行为,所以我觉得你有一点误解这里。你的 IntentService 是应用程序的组件。如果你杀了你的应用程序,然后它的所有部件都被杀害。

Because someone recommended I implemented an IntentService to do some work in the background. For now i just implemented a very basic service with some dummy code to pretend some long running work:

public class ImageSendEmailService extends IntentService {
    private static final int MY_NOTIFICATION_ID = 1;
    private NotificationManager notificationManager = null;
    private Notification notification = null;

    public ImageSendEmailService() {
        super("EmailService");
    }

    @Override
    public void onCreate() {
        super.onCreate();

        this.notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        for (int i = 0; i <= 10; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            String notificationText = String.valueOf((int) (100 * i / 10)) + " %";

            NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
            builder.setContentTitle("Progress");
            builder.setContentText(notificationText);
            builder.setTicker("Notification!");
            builder.setWhen(System.currentTimeMillis());
            builder.setDefaults(Notification.DEFAULT_SOUND);
            builder.setAutoCancel(true);
            builder.setSmallIcon(R.drawable.ic_launcher);

            this.notification = builder.build();
            this.notificationManager.notify(MY_NOTIFICATION_ID, this.notification);
        }
    }
}

Unfortunately the ui process in stopping always when i kill the app. If the progress for example is at 50% and i kill the app the progress stays at 50% and does not continue. The documentation says that an IntentService is not getting killed until its work is done but it gets killed in my case.

Later the IntentService should be used for several tasks:

  1. Sending Images using E-Mail
  2. Storing Images on a Server
  3. Repeating the Task automatically when the Task failed caused by missing internet connection.

It is also important to run in the background because i dont want the task to get interrupted when the user gets a phone call. and the repetition of the task is even more important. there could be temporally no connection to the internet, low battery status or even a crash of the whole phone.

解决方案

The documentation says that an IntentService is not getting killed until its work is done but it gets killed in my case.

I've checked the documentation and it says nothing about such behavior, so I think you have a little misunderstanding here. Your IntentService is the application's component. If you kill your app then all of its components are killed.

这篇关于IntentService正在停止每次我杀的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆