Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8480181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:13:35+00:00 2026-06-10T19:13:35+00:00

i need to disable mobile data connection programmatically i followed this private void setMobileDataEnabled(Context

  • 0

i need to disable mobile data connection programmatically

i followed this

private void setMobileDataEnabled(Context context, boolean enabled) {
    final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final Class conmanClass = Class.forName(conman.getClass().getName());
    final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
    iConnectivityManagerField.setAccessible(true);
    final Object iConnectivityManager = iConnectivityManagerField.get(conman);
    final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
    final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
    setMobileDataEnabledMethod.setAccessible(true);

    setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
}

StackOverFlow Link

but i got a Error “java.lang.NullPointerException:Println need a message”

Log

09-05 18:20:29.081: E/AndroidRuntime(736): FATAL EXCEPTION: Timer-0
09-05 18:20:29.081: E/AndroidRuntime(736): java.lang.NullPointerException: println needs a message
09-05 18:20:29.081: E/AndroidRuntime(736):  at android.util.Log.println_native(Native Method)
09-05 18:20:29.081: E/AndroidRuntime(736):  at android.util.Log.d(Log.java:138)
09-05 18:20:29.081: E/AndroidRuntime(736):  at com.anand.batterysaver.BatterySaverActivity.checkScreenStatus(BatterySaverActivity.java:69)
09-05 18:20:29.081: E/AndroidRuntime(736):  at com.anand.batterysaver.BatterySaverActivity$1.run(BatterySaverActivity.java:43)
09-05 18:20:29.081: E/AndroidRuntime(736):  at java.util.Timer$TimerImpl.run(Timer.java:284)

checkScreenStatus Function

 public void checkScreenStatus(){
        PowerManager powermanager;
        GetProcessList list = new GetProcessList();
        ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);

        powermanager = (PowerManager) this.getSystemService(Context.POWER_SERVICE); 
        if (powermanager.isScreenOn() == false)
        {
            Log.d("PowerStatus","Lock All Settings");
            boolean gprsstatus = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED;
            Log.d("Locked gprsStatus",String.valueOf(gprsstatus));
            if(gprsstatus == true){
                try{
                    list.setDataEnabled(getApplicationContext(), false);
                }catch (Exception e) {Log.d("Exception",e.getMessage()); }
            }
        }else{
            Log.d("PowerStatus","Resume All");
            gprsstatus = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED;
            Log.d("Locked gprsStatus",String.valueOf(gprsstatus));
            if(gprsstatus == true){
                TelephonyManager gprs = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
                if(gprs.getDataState() == TelephonyManager.DATA_CONNECTED){
                    Log.d("Gprs","on");
                }
            }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:13:37+00:00Added an answer on June 10, 2026 at 7:13 pm

    Try this :
    This works (this is a code of one of my apps on the market)

    private static void updateAPN(Context paramContext, boolean enable) {
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) paramContext.getSystemService("connectivity");
            Method setMobileDataEnabledMethod = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
            setMobileDataEnabledMethod.setAccessible(true);
            setMobileDataEnabledMethod.invoke(connectivityManager, enable);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    Bonus :

    private static boolean isAPNEnabled(Context paramContext) {
        try {
            NetworkInfo networkInfo = ((ConnectivityManager) paramContext.getSystemService("connectivity")).getActiveNetworkInfo();
            return networkInfo.isConnected();
        } catch (Exception e) {
            return false;
        }
    }
    

    By the way, don’t forget this :

    uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
    uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"
    uses-permission android:name="android.permission.INTERNET"
    uses-permission android:name="android.permission.READ_PHONE_STATE"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how can I disable and enable the mobile data(3G) on Android? I read that
I need to disable this entire script until the animation is complete so it
I need to add a feature to our mobile application which is to disable
I'm writing a filter that performs logging and I need to disable this logging
I need disable tooltip in google geo chart. I tried tooltip.trigger: 'none' and other
I need to disable access to a SharePoint application between 6 PM and 7
I need to disable check-in operation for several projects in my solution. I need
I need to disable IPv6. For that the java documentation indicates setting jvm property
I need to disable slickgrid horizontal scrollbar. I have only one column and have
For example I need to disable two buttons in runtime. After I disabled first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.