活动在后台时,Android服务无法连接到服务器

艾曼·哈桑·萨利姆(Ayman Hassan Salim)

这是我关于stackoverflow的第一篇文章,所以希望我能正确地进行所有操作。我正在开发我的第一个android应用程序,并且在某些设备上遇到奇怪的行为。我有一个前台服务,可以定期获取设备的位置,并使用带有延迟运行程序的处理程序将其发送到服务器。在我的手机(棉花糖,API 23)中,一切正常,但是当应用程序的活动处于后台时,使用运行Lollipop(API 21)的小米手机的朋友无法连接到服务器。这是将位置发送到服务器的功能:

    private void sendLocation(final String request_id, final String location ) {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(RestServiceConstants.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    String email = prefManager.pref.getString("UserEmail","");
    String password = prefManager.pref.getString("UserPassword","");

    Log.d(TAG,"sendLocation called");

    RestService service = retrofit.create(RestService.class);
    Call<StatusResponse> call = service.location("Basic "+ Base64.encodeToString((email + ":" + password).getBytes(),Base64.NO_WRAP),
            request_id,location);
    call.enqueue(new Callback<StatusResponse>() {
        @Override
        public void onResponse(Call<StatusResponse> call, Response<StatusResponse> response) {
            Log.d(TAG, "onResponse: raw: " + response.body());
            if (response.isSuccess() && response.body() != null){
                Log.d(TAG, "The location has been sent successfully");
                if(resendLocationHandler != null)
                    resendLocationHandler.removeCallbacksAndMessages(null);
            } else if (response.code() == 401){
                Log.i(TAG, "onCreate: User not logged in");
                prefManager.setIsLoggedIn(false);
            } else {
                Log.i(TAG, "sendLocation Unknown error occurred");
            }

        }

        @Override
        public void onFailure(Call<StatusResponse> call, Throwable t) {
            Log.i(TAG, "sendLocation Failed to get the server");
            if(resendLocationHandler == null)
                resendLocationHandler = new Handler();
            resendLocationHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    sendLocation(request_id, location);
                }
            }, resendFailedRequestDelay);

        }
    });
}

我不知道我还能提供什么来帮助您诊断问题,因此随时要求提出任何可能相关的内容。提前致谢

编辑:请求失败的意思是它触发了onFailure回调。回调中捕获的异常是:

java.net.ConnectException: Failed to connect to my_server_address:80
    at okhttp3.internal.io.RealConnection.connectSocket(RealConnection.java:139)
    at okhttp3.internal.io.RealConnection.connect(RealConnection.java:108)
    at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:188)
    at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:127)
    at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
    at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:289)
    at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:241)
    at okhttp3.RealCall.getResponse(RealCall.java:240)
    at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
    at okhttp3.RealCall.access$100(RealCall.java:30)
    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127)
    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:33)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:818)
尼尔·多尔科

它可能与某种“电池优化”有关,可能会阻止后台应用访问网络-例如,参阅https://www.reddit.com/r/Xiaomi/comments/4r6eld/does_battery_optimization_ever_work_for/

在MIUI上的后台应用出现问题后,我发现这与MIUI的节电功能有关。当我仅将“管理应用程序的电池使用情况”设置为标准时,即使后台应用程序具有自动运行权限,它们也很少能正常工作。“很少工作”是指:Google收件箱不同步,Google Maps不跟踪,某些基于位置的应用程序(如Rain Alarm)不显示降雨通知,依此类推。

我必须将所有这些应用程序都设置为例外列表,以便电池优化不会影响它们。[...]

因此,可能是小米用户(以及其设备具有相似功能的用户)需要手动将您的应用添加到白名单中。

我不知道默认情况下是否启用了此类“优化”设置-我当然希望它们没有启用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章