是否可以在onTaskRemoved中执行网络任务? [英] Is it possible to execute network tasks in onTaskRemoved?
问题描述
我需要在我的应用程序完全关闭(从backgrond清除)后将请求发布到服务器,并且当响应服务停止工作时,这是我的Service类:
I need when my app completely close (clear from backgrond) post my request to server and when got response service stop from working , this is my Service class :
public class OnClearFromRecentService extends Service {
private SharedPreferences prefs;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("ClearFromRecentService", "Service Started");
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("ClearFromRecentService", "Service Destroyed");
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Log.e("ClearFromRecentService", "END");
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(App.baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface requestInterface = retrofit.create(RequestInterface.class);
String authorization = prefs.getString("token_type","")+" "+prefs.getString("access_token","");
Call<JsonArray> response = requestInterface.AppOff(ChatList.offline_user,authorization);
response.enqueue(new Callback<JsonArray>() {
@Override
public void onResponse(Call<JsonArray> call, retrofit2.Response<JsonArray> response) {
String asd = "asd";
stopSelf();
}
@Override
public void onFailure(Call<JsonArray> call, Throwable t) {
Log.d(t.getLocalizedMessage(),"failed");
stopSelf();
}
});
}
清单中的定义:
<service android:name="com.iranMobleh.OnClearFromRecentService" android:stopWithTask="false" />
并从MainActivity启动服务,如下所示:
and start service from MainActivity like below:
startService(new Intent(this, OnClearFromRecentService.class));
看起来这段代码是正确的,但是问题是当应用关闭时,它不能等待改造响应
seem this code true but problem is when app closed it can't wait for retrofit response
推荐答案
首先,如果您设置 android:stopWithTask ="false"
,则不会调用 onTaskRemoved
.因此,将其设置为 android:stopWithTask ="true"
.请参见 服务文档 .
First of all if you set android:stopWithTask="false"
then onTaskRemoved
will not be called. So set it to android:stopWithTask="true"
. See Service Documentation.
现在我们转到解决方案.您必须将网络服务呼叫放入新的服务/工作中.并在 onTaskRemoved
被调用.
Now we move to the solution. You have to put your webservice call into a new Service/Job. and start that service/job by an AlarmManager
, or WorkManager
or EvernoteJobs
or JobScheduler
when onTaskRemoved
is called.
不要再写同样的东西.我发现两个很好的答案非常适合您的要求. 此 & 此 .
Instead of writing same thing again. I found two good answer perfectly fit for you requirement. This & This.
以上答案中只有一个更改.我认为EvernoteJobs或WorkManager会比AlarmManager更好.
Just one change in above answers. I think EvernoteJobs or WorkManager would be better than AlarmManager.
这很容易实现.
public class DemoSyncJob extends Job {
public static final String TAG = "job_demo_tag";
@Override
@NonNull
protected Result onRunJob(Params params) {
// run api call here
return Result.SUCCESS;
}
public static void runJobImmediately() {
int jobId = new JobRequest.Builder(DemoSyncJob.TAG)
.startNow()
.build()
.schedule();
}
}
访问 Evernote工作 页面进行集成.
Visit Evernote-jobs page for integrating.
更新
您必须使您的项目与库兼容.这是库使用的版本.因此,请确保您至少使用这些版本.
You have to make your project compatible with library. Here is the versions that library uses. So make sure you are using minimum these versions.
ext {
compileSdkVersion = 26
targetSdkVersion = compileSdkVersion
minSdkVersion = 14
buildToolsVersion = '27.0.3'
supportLibVersion = '26.1.0'
}
这篇关于是否可以在onTaskRemoved中执行网络任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!