gpt4 book ai didi

java - IntentService 失去与互联网的连接

转载 作者:太空宇宙 更新时间:2023-11-04 11:01:03 26 4
gpt4 key购买 nike

我的 IntentService 有问题。我需要我的服务在后台每 1 分钟访问网站中的 JSON 文件,而无需打开应用程序(为了发出通知,我以 JSON 形式收到通知)。它确实做到了这一点,但 3-4 分钟后,它抛出“java.net.SocketTimeoutException:连接超时”错误,我仍然可以将域解析为 IP 地址,但我无法连接到网站,并且手机仍然具有移动连接。所有网站都会发生同样的情况。

我的 Intent 服务:

public class BackgroundService extends IntentService {

Runnable runnable = () -> new bgJob().execute();
Handler handler = new Handler();
DatabaseHandler db = new DatabaseHandler(this);

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

@Override
public void onCreate() {
super.onCreate();
runnable.run();
}

@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
return START_STICKY;
}
@Override
public void onTaskRemoved(Intent rootIntent){
Log.d("BG_SERVICE","TASK REMOVED!");
Intent restartServiceTask = new Intent(getApplicationContext(),this.getClass());
restartServiceTask.setPackage(getPackageName());
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);
}

@Override
public void onDestroy() {
super.onDestroy();
Log.d("BG_SERVICE","TASK DESTROYED!");
Intent restartServiceTask = new Intent(getApplicationContext(),this.getClass());
restartServiceTask.setPackage(getPackageName());
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);
}

@Override
protected void onHandleIntent(Intent intent) {
WakefulBroadcastReceiver.completeWakefulIntent(intent);
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakelockTag");
wakeLock.acquire();
runnable.run();
}



private class bgJob extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
Request request = new Request.Builder()
.url("https://google.com/")
.build();
Response response = null;
try {
response = client.newCall(request).execute();
int webresponse = response.code();
Log.d("Response","CODE: "+webresponse);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
handler.postDelayed(runnable, 60000);
}
}}

我在具有移动连接的手机上使用 Android 7.0。

最佳答案

您必须按照 here 给出的说明进行操作据我所知,只有红米手机有这个问题。因此,请像这样在您的 Activity 中使用品牌名称。

String manufacturer = Build.MANUFACTURER;
String model = Build.MODEL;

使用此功能,您可以仅通知红米用户启用后台服务访问。

关于java - IntentService 失去与互联网的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46887008/

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