gpt4 book ai didi

当 Activity 在后台时,Android 服务无法连接到服务器

转载 作者:搜寻专家 更新时间:2023-11-01 08:30:37 27 4
gpt4 key购买 nike

这是我关于 stackoverflow 的第一篇文章,所以我希望我做的一切都是正确的。我正在开发我的第一个 android 应用程序,但我在某些设备上遇到了奇怪的行为。我有一个定期获取设备位置的前台服务,我使用带有延迟运行器的处理程序将其发送到服务器。在我的手机(Marshmallow,API 23)中一切正常,但是使用运行 Lollipop(API 21)的小米手机的 friend 在 App 的 Activity 处于后台时无法连接到服务器。这是将位置发送到服务器的函数:

    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/

after i had some problems with background apps on MIUI, i figured ithat something to do with the battery saver feature in MIUI. When ijust set "Manage apps' battery usage" to standard, the background appsrarely work, even if they have autorun permission. "Rarely work"means: Google Inbox does not sync, Google Maps does not track, somelocation based apps (like Rain Alarm) do not show rain notifications,and so on.

I had to set EVERY of these apps to the exception list, sothat the battery optimization does not affect them. [...]

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

我不知道像这样的“优化”设置是否在默认情况下处于启用状态——我当然希望它们不是。

关于当 Activity 在后台时,Android 服务无法连接到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41261575/

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