java - Android 在应用程序完全关闭之前向服务器发出请求

标签 java android android-intentservice android-ondestroy

我只有一个 Activity ,当用户关闭应用程序时(从操作系统清除最近应用程序的列表),我想向我的服务器 api 发送请求并更改用户状态。 所以我制作了 IntentService 并在 Activity 的 onDestroy() 方法中调用它,但它不起作用。怎么办呢?还有其他方法可以做到这一点(在应用程序完全终止之前向服务器发送请求)吗? 我的代码:

Activity :

@Override
protected void onDestroy() {
    Intent intent = new Intent(this, MakeOfflineIntentService.class);
    intent.putExtra(Variables.INTENT_TOKEN, Token);
    intent.setAction("ACTION_MAKE_OFFLINE");
    startService(intent);
    super.onDestroy();
}

在我的 IntentService 中:

public class MakeOfflineIntentService extends IntentService {

private static final String ACTION_MAKE_OFFLINE = "ACTION_MAKE_OFFLINE";

private static final String EXTRA_TOKEN = Variables.INTENT_TOKEN;

public MakeOfflineIntentService() {
    super("MakeOfflineIntentService");
}

public static void startActionFoo(Context context, String param1) {
    Intent intent = new Intent(context, MakeOfflineIntentService.class);
    intent.setAction(ACTION_MAKE_OFFLINE);
    intent.putExtra(EXTRA_TOKEN, param1);
    context.startService(intent);
}

@Override
protected void onHandleIntent(Intent intent) {
    if (intent != null) {
        final String action = intent.getAction();
        if (ACTION_MAKE_OFFLINE.equals(action)) {
            final String param1 = intent.getStringExtra(EXTRA_TOKEN);
            retrofitBaseInformationChange(param1,Variables.OFFLINE,1);
        }
    }
}

private void retrofitBaseInformationChange(final String Token, final int online, int vehicle){
    RetrofitCallServer retrofitCallServer = new RetrofitCallServer(WebServiceUrls.RETROFIT_INFORMATION_CHEETAH_MAN);
    OnCallBackRetrofit onCallBackRetrofit = retrofitCallServer.getResponse();

    Call<OBRbaseInfromationChange> call = onCallBackRetrofit.askBaseInformationChange(Token,online,vehicle);
    call.enqueue(new Callback<OBRbaseInfromationChange>() {

        @Override
        public void onResponse(Call<OBRbaseInfromationChange> call, Response<OBRbaseInfromationChange> response) {
            /*response gotten maybe success or not*/
            if (response.isSuccessful()){
                OBRbaseInfromationChange obr = response.body();
                if(obr.code == 200){
                    Log.i(Variables.APP_TAG,"BaseInformationChange successful");
                }
                else{
                    Log.i(Variables.APP_TAG,"BaseInformationChange error code: "+obr.code);
                }
            }// end if response successful
            else {
                Log.i(Variables.APP_TAG,"BaseInformationChange not Successful: "+response.code());
            }
        }

        @Override
        public void onFailure(Call<OBRbaseInfromationChange> call, Throwable t) {
            /*our request not sent or conversion problem*/
            Log.i(Variables.APP_TAG,"onFailure BaseInformationChange: "+t.getMessage());
        }

    });
}
// end retrofitBaseInformationChange()

}

最后这是我的 list :

<service
        android:name=".Services.MakeOfflineIntentService"
        android:exported="false"
        android:stopWithTask="false"/>

最佳答案

您是否尝试过在 onStartCommand 覆盖中返回 START_STICKY

发送请求后,您可以调用 stopService 来停止自己。

据我所知,当您终止应用程序时,即使是粘性服务也可能会“重新创建”。所以也许,Intent 并不是在这里使用的最佳方式。

我会在这里选择SharedPreferences:

  • 应用的 onCreate 将键“app_offline”设置为“false”

  • onDestroy 将此键设置为“true”并启动服务

  • 该服务为START_STICKY,当它发现“app_offline”为 true 时,会发送其请求,将“app_offline”更新为 false(重置),然后执行自行关闭。

类似这样的事情。 希望这有帮助,干杯,格里斯

关于java - Android 在应用程序完全关闭之前向服务器发出请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46070742/

相关文章:

java - Android WebView 不加载 URL?

android - Android上联系人的上次修改时间

java - SplashScreen finish() 应用程序最小化后

java - spring-data-mongo - 可选查询参数?

android - 从 JobService 观察 LiveData

java - 多个服务可以同时并行执行工作吗

android - 是否有任何开发模式可以替代网络请求的 IntentService?

java - 是否可以在一个数组中存储多个多维数组?

java - Jquery 缓动函数

android - 未收到通知的 PendingIntent