gpt4 book ai didi

Android JobIntentService 似乎重新启动了

转载 作者:行者123 更新时间:2023-11-29 23:49:18 26 4
gpt4 key购买 nike

我有一个监听电源连接事件的广播接收器。每当设备连接电源时,我都会尝试将文件从 APP 传输到运行 Ubuntu 的机器中的服务器。文件通过蓝牙传输。由于文件传输很重要,如果出于任何原因传输出现错误,或者第一次尝试连接不成功,我最多重试 6 次,尝试间隔 3 分钟。

一开始,我使用的是一个异步任务,只要我们仍然可以重试并且文件传输没有成功完成,它就会保持 Activity 状态。我读到,在广播接收器中使用 asynctask 并不是一个很好的做法,这完全有道理,特别是因为我强制任务长时间运行。因此,我决定更改为 JobIntentService,以便每次接收器捕获到电源连接事件时,我都会发出将文件传输到我的计算机的作业。在作业中,在文件传输完成或失败后,我会立即设置一个警报,向广播发送一个待处理的 Intent 并再次调用该作业。

我正在运行它并且我注意到(与以前不同)我在传输过程中遇到了太多“连接被对等重置”错误,这让我想知道作业是否在完成之前就被停止了还是什么像那样?。在我之前的实现中,这些错误过去不会发生。然后,我还注意到,出于某种原因,操作系统似乎再次自行启动了 JobIntentService(没有启动它的事件),这导致我的代码不一致并导致我丢失了一些文件(我不应该允许此作业的多个实例同时运行)

我的问题是,您认为服务重启的原因是什么?是否有可能在 BT 传输期间由操作系统完成并重新启动 JobIntentService?这些文件很重,因此需要几分钟才能从应用程序传输到机器。我正在考虑尝试前台服务而不是 JobIntent 并收到服务通知或返回到我之前的实现。

有什么建议吗?

这就是我对 Intent Job 的称呼。

FileTransferJob.isJobAlreadyRunning = true;
Intent intent = new Intent(context, FileTransferJob.class);
intent.putExtra(TRANSFER_DATA_RETRIES, retries);
FileTransferJob.enqueueWork(context,intent);

这是 JobIntentService 类

public class FileTransferJob extends JobIntentService {
/**
* Unique job ID for this service.
*/
public static boolean isJobAlreadyRunning = false; //This flag will remain true as soon as this JOB is called and as long as retries are still available
public static final int JOB_ID = 1000;
public static int MAX_NUM_OF_RETRIES = 6;//How many times are we going to retry to send the data
private int MINUTES_TO_WAIT = 3; //The minutes we wait between each attempt
public String TAG = "FileTransferJob";

/**
* Convenience method for enqueuing work in to this service.
*/
public static void enqueueWork(Context context, Intent work) {
enqueueWork(context, FileTransferJob.class, JOB_ID, work);
}

@Override
protected void onHandleWork(Intent intent) {

int retriesRemaining = intent.getIntExtra(TRANSFER_DATA_RETRIES,1); //Get the number of retries we have. Default to 1 (this one)
Log.d(TAG, "onHandleWork: About to attempt transfer with remaining retries " + String.valueOf(retriesRemaining));


try {
BluetoothFileTransfer btio = new BluetoothFileTransfer();
Log.d(TAG, "onHandleWork: About to send data over Bluetooth");
btio.sendData(FileTransferJob.this.getApplicationContext());
FileTransferJob.isJobAlreadyRunning = false; //Success, then this is no longer running
Log.d(TAG, "onHandleWork: The data has been sent over Bluetooth");
}catch (Exception e){
Log.d(TAG, "onHandleWork: There was a problem with the BT transfer: " + e.getMessage());

retriesRemaining--; //We reduce the number of retries we have

//If no more retries available, simply do nothing
if (retriesRemaining > 0) {
Log.d(TAG, "onHandleWork: Setting up alarm. Retries ramaining: " + String.valueOf(retriesRemaining));
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(this.getApplicationContext(), DataCollectReceiver.class);
alarmIntent.setAction(TRANSFER_DATA);
alarmIntent.putExtra(TRANSFER_DATA_RETRIES, retriesRemaining);

PendingIntent alarmPendingIntent = PendingIntent.getBroadcast( this.getApplicationContext(), PENDING_INTENT_CODE_FILE_TRANSFER_JOB, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
int totalTime = MINUTES_TO_WAIT*60*1000;
if(alarmManager != null){
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + totalTime,
alarmPendingIntent);
Log.d(TAG, "onHandleWork: Alarm is set, waiting " + String.valueOf(totalTime) + " minutes for next attempt...");
}else{
Log.d(TAG, "onHandleWork: Alarm could not be set. Alarm manager is NULL");
}

}else{
Log.d(TAG, "onHandleWork: There are no more retries");
FileTransferJob.isJobAlreadyRunning = false;
}
}
}

@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: The file transfer JOB has finished");
}

}

日志。突出显示的部分显示了我认为操作系统正在创建并运行 JobService 的新实例。

enter image description here

最佳答案

让我试着回答一下,因为我注意到了这种行为。 JobIntentService/JobService/Worker 将仅运行 10 分钟,之后它们将停止,您可以在 JobService/JobIntentService 的情况下在 onStopJob/onStopCurrentWork 上获得回调,在 Worker 的情况下获得 OnStopped 回调。

虽然 android 文档仅针对 Worker 解释了此行为,但 JobService/JobIntentServie 的行为方式相同

A Worker is given a maximum of ten minutes to finish its execution and return a ListenableWorker.Result. After this time has expired, the Worker will be signalled to stop.

因此我可以假设您的任务没有在 10 分钟内完成并且 Android 正在破坏 JobIntentService。现在的问题是,所有这些 Jobservice/JobIntentService/Worker 在指数退避时间后再次启动(如果过早停止),即 30secs、1 min、2 mins、4 mins...

虽然奇怪的部分是运行 10 分钟后死掉的旧线程按照解释启动,但是当回调再次出现在 HandleWork 上时,它再次启动另一个线程,它重复线程完成的工作,这就是我认为你的原因查看不一致之处。

建议您以可以在 10 分钟窗口内完成的方式中断工作。或者我们可以等待 Google 团队解决此问题。

关于Android JobIntentService 似乎重新启动了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51072308/

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