gpt4 book ai didi

如果设备空闲,Android 前台服务会变慢

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:20:53 26 4
gpt4 key购买 nike

我有一个 android 前台服务,通过通知调用。在服务中,我只是每 10 秒记录一次“Tick tack”,但服务的优先级是每 X 秒在 web View 中导航,因此我也在使用新线程并在主线程中工作。

如果我将应用程序连接到 USB,日志似乎没问题,每 10 秒调用一次“tick tack”,如果手机已解锁并且我在应用程序上查看日志也是如此。

但是当我断开 USB 连接,或者我锁定设备时,就会发生这种情况:

2018-11-14 12:11:53.115 12596-12596/? I/Service: tick tack
2018-11-14 12:12:18.704 12596-12596/? I/Service: tick tack
2018-11-14 12:15:42.572 12596-12596/? I/Service: tick tack
2018-11-14 12:17:30.942 12596-12596/? I/Service: tick tack
2018-11-14 12:17:53.534 12596-12596/? I/Service: tick tack
2018-11-14 12:18:27.118 12596-12596/? I/Service: tick tack
2018-11-14 12:18:37.118 12596-12596/? I/Service: tick tack
2018-11-14 12:18:47.118 12596-12596/? I/Service: tick tack
2018-11-14 12:18:57.121 12596-12596/? I/Service: tick tack
2018-11-14 12:19:25.208 12596-12596/? I/Service: tick tack
2018-11-14 12:19:48.294 12596-12596/? I/Service: tick tack

前台服务的限制是什么?即使设备处于空闲状态,我也可以进行繁重的前台工作吗?

最佳答案

前几天我为类似的东西制作了这个库 service in background and foreground

而且它的工作绝对没有问题。

我选择使用 AlarmManager 运行它的原因是如果应用程序需要执行本地事件,AlarmManager 是一个很好的调度候选者 + 允许应用程序安排可能需要运行或重复超出其生命周期范围的任务。这允许应用程序执行某些功能,即使在应用程序进程或其所有 Android 组件已被系统清理后也是如此。

更新

调用该方法启动服务

 public void call(int Value_in_seconds) {
if (Value_in_seconds == (int) Value_in_seconds) {
// Number is integer
Long time = new GregorianCalendar().getTimeInMillis() + Value_in_seconds * 1000;
// create an Intent and set the class which will execute when Alarm triggers, here
// ServiceReciever in the Intent, the onRecieve() method of this class will execute when
// alarm triggers
Intent intentAlarm = new Intent(context, ServiceReciever.class);
// create the object
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
//set the alarm for particular time
alarmManager.set(AlarmManager.RTC_WAKEUP, time, PendingIntent.getBroadcast(context, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
} else {
Toast.makeText(context, context.getString(R.string.intValue), Toast.LENGTH_SHORT).show();
}
}

创建ServiceReciever

public class ServiceReciever extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
//call the method here

}
}

在您的 list

<application>
<receiver android:name="hossamscott.com.github.backgroundservice.ServiceReciever" android:process=":ff" android:exported="true" android:enabled="true">
</receiver>

<service android:name="hossamscott.com.github.backgroundservice.BackgroundTask"/>

</application>

如果你喜欢在 Thread 中运行它,那就应该是这样比你可以添加下一行

public class BackgroundTask extends Service {

private boolean isRunning;
private Context context;
private Thread backgroundThread;


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

@Override
public void onCreate() {
this.context = this;
this.isRunning = false;
this.backgroundThread = new Thread(myTask);
}

private Runnable myTask = new Runnable() {
public void run() {
// Do something here
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
// do your logic here
}
});
stopSelf();
}
};

@Override
public void onDestroy() {
this.isRunning = false;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!this.isRunning) {
this.isRunning = true;
this.backgroundThread.start();
}
return START_STICKY;
}

}

要调用这个 classServiceReciever 编辑成这样

 @Override
public void onReceive(Context context, Intent intent) {
//call the method here
Intent background = new Intent(context, BackgroundTask.class);
context.startService(background);
}

关于如果设备空闲,Android 前台服务会变慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53299557/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com