Android的服务启动后立即死亡,尽管调用startForeground() [英] Android service killed immediately after start, despite calling startForeground()

查看:1151
本文介绍了Android的服务启动后立即死亡,尽管调用startForeground()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的IntentService一个问题。每次我开始我的服务,的onDestroy()方法,只要服务变为空闲调用。设置我的服务,在前台运行,尽管该服务仍然被杀害的时候了。我只有一个其他活动在我的应用程序,它并没有叫stopService()。

阅读开发商的文档给我的IM pression,调用startForeground()将让您的服务持续下去,即使在空闲时,除非有对内存的要求非常高,还是我读这错了吗?

我的code以下:

 公共类FileMonitorService扩展IntentService {
  公众诠释mNotifyId = 273;
  公共FileMonitorService(){
    超级(FileMonitorService);
}@覆盖
保护无效onHandleIntent(意向为arg0){}@覆盖
公共无效的onDestroy(){
    Toast.makeText(此,它是gettext(R.string.toast_service_stop),Toast.LENGTH_SHORT).show();
    stopForeground(真);
    super.onDestroy();
}@覆盖
公众诠释onStartCommand(意向意图,诠释标志诠释startId){
    通知通知=新的通知(R.drawable.icon,它是gettext(R.string.notification_short),System.currentTimeMillis的());
    notification.flags | = Notification.FLAG_NO_CLEAR;
    意图notificationIntent =新意图(这一点,FileMonitorActivity.class);
    的PendingIntent的PendingIntent = PendingIntent.getActivity(在此,0,notificationIntent,0);
    notification.setLatestEventInfo(此,它是gettext(R.string.notification_short),它是gettext(R.string.notification_long)的PendingIntent);
    startForeground(mNotifyId,通知);
    Toast.makeText(此,它是gettext(R.string.toast_service_start),Toast.LENGTH_SHORT).show();
    返回super.onStartCommand(意向,旗帜,startId);
}
  }


解决方案

您需要考虑使用普通的服务代替 IntentService IntentService 旨在保持运行时,它的工作要做。一旦你完成了你的 onStartCommand 方法,试图阻止。

查看文档


  

客户端通过startService(意向)调用发送的请求;根据需要启动服务,使用工作线程处理每个意向反过来,和当它运行的工作停止本身。


(重点煤矿)

I'm having a problem with my IntentService. Every time I start my service, the onDestroy() method is called as soon as the service becomes idle. I set up my service to run in the foreground, and despite this the service is still being killed right away. I have only one other activity in my application, and it is not calling stopService().

Reading the developer docs gives me the impression that calling startForeground() will allow your service to persist, even when idle, except when there is an very high demand for memory, or am I reading this wrong?

My code below:

public class FileMonitorService extends IntentService {
  public int mNotifyId = 273;
  public FileMonitorService(){
    super("FileMonitorService");
}

@Override
protected void onHandleIntent(Intent arg0) {

}

@Override
public void onDestroy() {       
    Toast.makeText(this, getText(R.string.toast_service_stop), Toast.LENGTH_SHORT).show();
    stopForeground(true);
    super.onDestroy();      
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {      
    Notification notification = new Notification(R.drawable.icon, getText(R.string.notification_short), System.currentTimeMillis());
    notification.flags|=Notification.FLAG_NO_CLEAR;     
    Intent notificationIntent = new Intent(this, FileMonitorActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.setLatestEventInfo(this, getText(R.string.notification_short),getText(R.string.notification_long), pendingIntent);
    startForeground(mNotifyId, notification);


    Toast.makeText(this, getText(R.string.toast_service_start), Toast.LENGTH_SHORT).show();
    return super.onStartCommand(intent, flags, startId);
}   
  }

解决方案

You need to look into using a regular Service instead of an IntentService. IntentService is designed to keep running while it has work to do. Once you've finished your onStartCommand method, it tries to stop.

See the docs:

Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.

(Emphasis mine)

这篇关于Android的服务启动后立即死亡,尽管调用startForeground()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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