简体   繁体   中英

IntentService loses connection with internet

I have problem with my IntentService. I need my service to access JSON file in website every 1 minute in background, without app being open (To make notification, which I receive in JSON). It does that, but after 3-4 minutes, it throws " java.net.SocketTimeoutException: connect timed out " error and I can still resolve domain to IP address, but I cannot connect to website, and phone still have mobile connection. Same thing happens with all websites.

My IntentService:

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);
    }
}}

I am using Android 7.0 on my phone with mobile connection.

You have to follow the instructions given here As for as my knowledge is concerned only redmi mobiles have this problem. So get the brand name in your activity like this.

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

and using this you can notify only the redmi users to enable background service access.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM