开发者

android service stops working after application is not in use for awhile

I have a service for my application that runs fine开发者_Go百科, but when a user is not using there phone for while like 20 minutes the service just doesnt work any more. is there something i suppose to be doing like saving a state or something, i am lost at this point. I dont see why the service doest keep running, I look in the application>running services on my phone and it still says its running. Any suggestions?


I faced the same problem few time back. Then I started to use Android Alarm Service. That solved my problem. Here is a reference link http://android-er.blogspot.com/2010/10/simple-example-of-alarm-service-using.html


If you want to keep running in the background (e.g. downloading something), you need to grab a wakelock (http://developer.android.com/reference/android/os/PowerManager.html)

Also, look at the following if you happen to do downloads in the background: http://developer.android.com/reference/android/net/ConnectivityManager.html#getBackgroundDataSetting()


You can use Broadcast receiver instead to use like service. And it can be called same as Service with alarm manager. Receiver:

public class CallListReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Toast.makeText(context, "Call List receiver called", Toast.LENGTH_LONG)
            .show();

    }
}

You can continuously call it using:

public void startAlert(Context context) {
Intent call_intent = new Intent(this, CallListReceiver.class);
    PendingIntent pendingCallIntent = PendingIntent.getBroadcast(context,
            0, call_intent, 0);
    AlarmManager call_alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    call_alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
            System.currentTimeMillis(), (7 * 1000), pendingCallIntent);
}


This Android's behavior that A Service has maximum life of 20 minutes. After 20 Minutes, it will be automatically killed by Android OS. But i think that there is a solution. i dont have tested it yet. The Solution is: In your onStartCommand(), put this code at the end of this method return START_STICKY;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜