应用程序暂停使用一段时间后,android服务停止工作

问题描述 投票:6回答:4

我有一个服务,我的应用程序运行正常,但当用户没有使用那里的电话,如20分钟,该服务不再工作。是否有一些我认为要做的事情就像拯救一个国家或某事一样,我现在迷失了。我不明白为什么服务doest继续运行,我查看应用程序>在我的手机上运行服务,它仍然说它运行。有什么建议?

android service
4个回答
5
投票

我几次回来时遇到了同样的问题。然后我开始使用Android Alarm Service。这解决了我的问题。这是一个参考链接http://android-er.blogspot.com/2010/10/simple-example-of-alarm-service-using.html


4
投票

如果你想继续在后台运行(例如下载东西),你需要拿一个唤醒锁(http://developer.android.com/reference/android/os/PowerManager.html)

另外,如果您碰巧在后台进行下载,请查看以下内容:http://developer.android.com/reference/android/net/ConnectivityManager.html#getBackgroundDataSetting()


4
投票

您可以使用广播接收器来使用类似的服务。并且它可以与警报管理器的服务相同。接收器:

public class CallListReceiver extends BroadcastReceiver {

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

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

    }
}

您可以使用以下方式连续调用它

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);
}

3
投票

这个Android的行为表明A服务的最长寿命为20分钟。 20分钟后,它将被Android操作系统自动杀死。但我认为有一个解决方案。我还没有测试过它。解决方案是:在你的onStartCommand()中,将此代码放在此方法的末尾return START_STICKY;

© www.soinside.com 2019 - 2024. All rights reserved.