【问题标题】:Android onTaskRemoved() call to webserviceAndroid onTaskRemoved() 调用网络服务
【发布时间】:2016-12-24 06:13:52
【问题描述】:

美好的一天。我的情况很糟糕。我正在会话中创建位置共享逻辑。我将该会话保持在 mysql 上的服务器上。当 android 遇到该活动时,我会插入相应的用户信息。当 android 离开活动时我删除了该列,因此会话被放弃给另一方。一切都很好,直到一个问题。Android 不为从最近刷卡的应用程序提供回调,这意味着应用程序被完全杀死。我在那里找到了一个工作。我正在使用服务并在达到我想要的活动后立即启动服务。在服务中,我有一个简单的东西叫做 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 的日志,仅此而已..有人知道发生了什么吗?我该如何处理这种情况?

【问题讨论】:

  • 您好,您有解决此问题的方法吗?谢谢。
  • 还没有 :( 无法通过

标签: android service kill-process


【解决方案1】:
/**
 * 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 服务的想法,并在意图中放置了一些额外内容。通过这种方式,您可以区分何时重新启动服务以执行您的 API 调用。

【讨论】:

  • 它帮助了我。但我首先检查了意图是否不为空,然后做一些事情。这是我的代码:@Override public int onStartCommand(Intent intent, int flags, int startId) { if(intent!=null) { Bundle bundle = intent.getExtras(); if (bundle != null) { final String logout = bundle.getString("logout"); if (logout != null) { logoutAndStopServiceRightAway();返回 START_NOT_STICKY; } } } 返回 START_STICKY; }
  • 由于某种原因,代码无法在运行 android 10 的 oneplus 5 上运行,而在运行 7.1.1 的平板电脑 samsung tab 7 上运行正常
  • 这里也一样。在 android 8 中,服务重新启动,但 Intent extra 为空。
【解决方案2】:

我通过取消线程策略的严格模式解决了这个问题。所以http请求可以从主线程完成。

 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
 StrictMode.setThreadPolicy(policy);

一般情况下不建议这样做,但是当应用程序崩溃或关闭时,我想它可以发送一个 http 请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多