Android IntentService OnHandle设备关闭时的内容行为
onHandleIntent on device shutdown的行为是什么 我知道在IntentService上,只要OnHandleContent没有完成它的工作,服务就会一直运行 因此,仔细想想,这是一个关于设备关闭时服务行为的一般性问题,一旦我们再次启动设备,它们会“醒来”吗 如果没有,有办法吗?我希望我的intentservice一直运行到onHandleIntent完成,不管发生了什么 编辑: 为了更好地理解,我将添加代码。 我试图在SQLite上保存请求并一直运行,直到该表为空,然后服务将关闭。 因此,在设备关闭的情况下,我仍将从关机前的相同位置继续。 另外,我尝试使用executor以获得更好的性能(这是一个测试,尚未得到验证) 如果有人有更好的建议,我很乐意听他们的建议,因为我在这方面是新手 手感Android IntentService OnHandle设备关闭时的内容行为,android,device,shutdown,android-intentservice,application-shutdown,Android,Device,Shutdown,Android Intentservice,Application Shutdown,onHandleIntent on device shutdown的行为是什么 我知道在IntentService上,只要OnHandleContent没有完成它的工作,服务就会一直运行 因此,仔细想想,这是一个关于设备关闭时服务行为的一般性问题,一旦我们再次启动设备,它们会“醒来”吗 如果没有,有办法吗?我希望我的intentservice一直运行到onHandleIntent完成,不管发生了什么 编辑: 为了更好地理解,我将添加代码。 我试图在SQLite上保存请求并一直运行,直到该表为空,
@Override
protected void onHandleIntent(Intent intent) {
helper = new DBHelper(getApplicationContext());
executor = Executors.newFixedThreadPool(5);
File file;
Log.e("requestsExists",helper.requestsExists()+"");
while (helper.requestsExists()) {
ArrayList<String> requestArr = helper.getRequestsToExcute(5);
//checks if the DB requests exists
if (!requestArr.isEmpty()) {
//execute them and delete the DB entry
for(int i = 0; i < requestArr.size(); i++) {
file = new File(requestArr.get(i));
Log.e("file",file.toString());
Future<String> future = executor.submit(new MyThread(file,getApplicationContext()));
Log.e("future object", future.toString());
try {
long idToDelete = Long.parseLong(future.get());
Log.e("THREAD ANSWER", future.get() + "");
helper.deleteRequest(idToDelete);
} catch (InterruptedException e) {
Log.e("future try", "");
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
}
executor.shutdown();
}
我将试着逐点回答你的问题 onHandleIntent on device shutdown的行为是什么? 由于设备将完全关闭,所有服务将被强制和停止,包括您的IntentService。
您可以订阅以了解设备何时将关闭,并在设备实际关闭之前执行一些操作
public class ShutdownHandlerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Handle here, but don't make actions that takes too long
}
}
您还必须将其添加到清单中
<receiver android:name=".ShutdownHandlerReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
</intent-filter>
</receiver>
在舱单上:
<receiver android:name=".BootedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
然后你可以重新启动你的intenter服务,我想这是最好的方法 好的,当设备关闭时,您的IntentService将停止。如果设备当时正在运行,系统在引导时不会自动为我们启动,我们必须自己完成
请参阅以了解如何操作。为什么要使用IntentService,为什么不使用普通服务?嗯,抱歉,我不知道如果您在工作线程中提交作业,为什么需要后台线程。我稍后添加了工作线程以尝试提高性能。我想这也可以通过服务来完成。谢谢!我试试这个。为了更好地理解,广播接收者基本上只会从我在清单上分配给他的特定操作中被调用?因为启动和关闭都只会扩展他,区别在清单上?
public class BootedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Handle here, you can start again your stopped intentservices
}
}
<receiver android:name=".BootedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>