Android Service: Detect if data connection is available either Wifi or Mobile Data

2011 views android
2

How can I detect continuously or by schedule if data connection is available either Wifi or Mobile Data. I need to detect it even if the app gets in foreground, background, or get killed by the system or user. I am new to this and already seen a lot of relevant question and sample code but I am not sure where to start since they say that things have change in Android N and above for security reason. Is there any sample or library that is free and available?

answered question

do you have any idea about service in android>?

yes I already have some idea with it

you want connection available or connection connected?

connected sorry for the mistake

1 Answer

6

This is your service class working fine just add network state permission in your manifest and at runtime

public class MyService extends Service {
    private static final String TAG = "MyService";
    private ServiceHandler serviceHandler;
    private static final int MSG_REQUEST = 1112;
    public static void startService(Context context) {
        Intent myIntent = new Intent(context, MyService.class);
        context.startService(myIntent);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    private class ServiceHandler extends Handler {
        ServiceHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_REQUEST:
                    checkWifi();
                    break;
            }
        }
    }

    private void checkWifi() {
    ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (mWifi.isConnected()) {
// Do whatever
    }


    }

    @Override
    public void onCreate() {
        super.onCreate();
        HandlerThread handlerThread = new HandlerThread(TAG, Process.THREAD_PRIORITY_BACKGROUND);
        handlerThread.start();
        serviceHandler = new ServiceHandler(handlerThread.getLooper());
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (!serviceHandler.hasMessages(MSG_REQUEST)) {
            serviceHandler.sendEmptyMessage(MSG_REQUEST);
        }


        return START_REDELIVER_INTENT;
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);

        PendingIntent service = PendingIntent.getService(
                getApplicationContext(),
                1001,
                new Intent(getApplicationContext(), MyService.class),
                PendingIntent.FLAG_ONE_SHOT);

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        assert alarmManager != null;
        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000, service);
    }

}

Now call the service from Activity

MyService.startService(MainActivity.this);

posted this

Have an answer?

JD

Please login first before posting an answer.