gpt4 book ai didi

Android onTaskRemoved() 调用网络服务

转载 作者:行者123 更新时间:2023-11-29 17:16:01 26 4
gpt4 key购买 nike

美好的一天。我遇到了可怕的情况。我正在 session 中创建一个位置共享逻辑。我将该 session 保存在 mysql 上的服务器上。当 android 命中该 Activity 时,我插入相应的用户信息。当 android 离开 Activity 时我删除了那一列,这样 session 就被放弃了。在一个问题之前一切都很好。Android 不为应用程序从最近刷过的应用程序提供回调,这意味着应用程序被完全杀死。我在那里找到了一个工作。我正在使用一个服务并在它达到我想要的 Activity 时立即启动服务。在服务中我有一个简单的东西叫做 onTaskRemoved() 它会在应用程序被从最近的应用程序中删除时立即通知我。一切都很好,直到我想打电话到我的服务器以删除该列。调用不仅会通过,我永远不会在那里收到任何响应,但在 onDestroy() 中,一切都按预期工作。实际上这里是代码

 @Override
public void onTaskRemoved(Intent rootIntent) {
destroySession();
super.onTaskRemoved(rootIntent);
}

private void destroySession() {
Log.d("Fsafasfsafasfas", "destroySession: " + opponentId + " my user id" + sharedHelper.getUserId());
Call<ResponseBody> locationCall = Retrofit.getInstance().getInkService().requestFriendLocation(sharedHelper.getUserId(), opponentId, "", "", Constants.LOCATION_REQUEST_TYPE_DELETE);
locationCall.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response == null) {
destroySession();
return;
}
if (response.body() == null) {
destroySession();
return;
}
try {
String responseBody = response.body().string();
Log.d("Fsafasfsafasfas", "onResponse: " + responseBody);
JSONObject jsonObject = new JSONObject(responseBody);
boolean success = jsonObject.optBoolean("success");
if (success) {
stopSelf();
} else {
destroySession();
}
} catch (IOException e) {
stopSelf();
e.printStackTrace();
} catch (JSONException e) {
stopSelf();
e.printStackTrace();
}
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
destroySession();
}
});
}

我猜这个电话永远打不通,因为唯一打印的日志是 id 的日志,仅此而已。任何人都知道发生了什么事?我该如何处理这种情况?

最佳答案

/**
* Created by Parag on 01/05/2017.
*/

public class AppService extends android.app.Service {
public static final String TAG=AppService.class.getName();
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle bundle=intent.getExtras();
if(bundle!=null){
final String logout=bundle.getString("logout");
final String driverLoginId=bundle.getString("driverLoginId");
if(logout!=null&&driverLoginId!=null){
Toast.makeText(this, "logging out", Toast.LENGTH_SHORT).show();
Log.e(TAG,"Logging out");
Log.e(TAG,"Inside driverLogout "+driverLoginId);
Call<LogoutResponse> call = RestHandler.getApiService().driverLogout(driverLoginId);
call.enqueue(new Callback<LogoutResponse>() {
@Override
public void onResponse(Call<LogoutResponse> call, Response<LogoutResponse> response) {
//close the service on receiving response from API
Log.e("Response : ",response.body().getStatus()+"");
AppService.this.stopSelf();
}
@Override
public void onFailure(Call<LogoutResponse> call, Throwable t) {
//close the service on receiving response from API
AppService.this.stopSelf();
}
});

}else{
//Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
Log.e(TAG,"DriverLoginId : "+driverLoginId);
Log.e(TAG,"Logout : "+logout);
}
}else{
//Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
Log.e(TAG,"Service Start");
}
return super.onStartCommand(intent,flags,startId);
}

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

@Override
public void onTaskRemoved(Intent rootIntent) {

Log.e(TAG,"Service Stop");
UserLocalStore userLocalStore=new UserLocalStore(AppService.this);
Log.e("USER DATA :",userLocalStore.fetchUserData().toString());
Intent restartServiceTask = new Intent(getApplicationContext(),this.getClass());
restartServiceTask.setPackage(getPackageName());
restartServiceTask.putExtra("logout","true");
restartServiceTask.putExtra("driverLoginId",userLocalStore.fetchUserData().getUserId());
PendingIntent restartPendingIntent =PendingIntent.getService(getApplicationContext(), 1,restartServiceTask, PendingIntent.FLAG_ONE_SHOT);
AlarmManager myAlarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
myAlarmService.set(
AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 1000,
restartPendingIntent);
super.onTaskRemoved(rootIntent);
}


}

我也面临同样的问题,即无法从 onTaskRemoved 方法进行任何 API 调用。所以,我也研究了很多,但没有找到解决办法。所以,我终于想到了重新启动 Web 服务,并在 Intent 中放置一些额外的东西。通过这种方式,您可以区分服务何时重新启动以执行您的 API 调用。

关于Android onTaskRemoved() 调用网络服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38999142/

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