0

i am working on an tracking app where i needs to capture mobile data enable/disable event. i am successfully able to get internet enable/disable broadcast by the following code:

 private BroadcastReceiver networkInfoReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            ConnectivityManager connect = (ConnectivityManager) getSystemService(getBaseContext().CONNECTIVITY_SERVICE);
            NetworkInfo network = connect.getActiveNetworkInfo();
            if (network == null || !network.isConnected()) {
                sharedPreferencesEditor.putBoolean(Constants.INTERNET_STATUS,false);
            } else {
                sharedPreferencesEditor.putBoolean(Constants.INTERNET_STATUS,true);
            }
            sharedPreferencesEditor.commit();

        }

but by using this code snippet i got only internet status. it will be fire in both conditions whether data disable or no network Area. but i needs a broadcast which fires only when mobile data disable manually.

1 Answers1

0

Try below code :

        boolean mobileDataEnabled = false; 
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        try {
            Class cmClass = Class.forName(cm.getClass().getName());
            Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
            method.setAccessible(true); 
            mobileDataEnabled = (Boolean)method.invoke(cm);
        } 
        catch (Exception e) {         
            // TODO do whatever error handling you want here
        }
Android Geek
  • 8,956
  • 2
  • 21
  • 35