0

My application sends gps updates to a API. I need my application to run in background all the time. But unfortunately, my application always end at some point while on background. I have read that when the cpu usage of the application is low, the application is will be killed automatically. I don't want this to happen. I already included a partial wake lock on my onCreate method in my application using this code:

 powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
 wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");

Then on pause:

protected void onPause()
 {
     super.onPause();
     onBackground = true;
     wakeLock.acquire();
     Log.w("OnPause", "OnPause");
 }

I really don't know how to prevent my application being killed. I also tried using full wake lock but it is deprecated. Any ideas on how will I keep my application alive on background? I never want my application to be killed while on background. Thanks!

4

2 回答 2

3

你不能。

只要确保,你坚持任何应该在被杀后幸存下来的东西。

对于应该在后台运行的任何内容,即使应用程序未向用户显示:
使用 aService并将其设置为前台模式。

于 2013-09-18T04:09:01.260 回答
0
public class BGService extends Service {

    private PowerManager powerManager;
    private WakeLock wakeLock;

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();

        powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                "MyWakeLock");
        wakeLock.acquire();
    }

在您的主要活动中调用并启动服务

public class SendSMSActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo);
        startService(new Intent(this, BGService.class));
    }

}
于 2013-09-18T05:17:57.297 回答