简体   繁体   English

Android onStartCommand服务

[英]Android onStartCommand Service

I have a service that I am trying to run constantly as long as the user has it on. 只要用户打开了我的服务,我就会尝试使其持续运行。 The problem is the service uses a ton of memory; 问题在于服务使用了大量的内存。 I'm guessing because it notifies the users and displays images constantly. 我猜是因为它会通知用户并不断显示图像。 So I put the bulk of my code in another activity. 因此,我将大部分代码放在了另一个活动中。 The service just calls the activity. 该服务仅调用活动。

The issue is that I'm trying to use the return Start_Sticky to restart the service when it needs to. 问题是我试图在需要时使用return Start_Sticky重新启动服务。 It takes about 2 hours before it uses up enough memory to need to restart. 它需要大约2个小时才能用尽足够的内存来重新启动。 When it does restart it doesn't do the onStartCommand am I missing something? 当它重新启动时,它不会执行onStartCommand吗?

public class AUTOAlarmService extends Service {
@Override
public void onCreate() {
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Intent DI = new Intent(getBaseContext(), AUTOSERVICES.class);
    DI.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    getApplication().startActivity(DI);
    return START_STICKY;
}

@Override
public boolean onUnbind(Intent intent) {
    return super.onUnbind(intent);
}

} }

I think you should reconsider some work done because your service is taking too much resources and that shouldnt be happening any way. 我认为您应该重新考虑已完成的某些工作,因为您的服务占用了太多资源,并且不应该以任何方式发生。 There are many applications with a lot of services but non have problem of restarting the device for that matter. 有许多应用程序具有很多服务,但与此无关,没有重启设备的问题。

However if you want to restart a service you have to stop and then start the service again. 但是,如果要重新启动服务,则必须先停止然后重新启动该服务。

stopService(new Intent(this, YourService.class));
startService(new Intent(this, YourService.class));

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM