3

I wish to detect if a user has enabled both background data (settings->accounts and sync->background data) and packet data (settings->wireless and network->mobile networks->use packet data) so I can inform the user how to enable them.

This link says how to test the background data but it has been deprecated. The recommendation says to use getActiveNetworkInfo() but this might return the WIFI connection and therefore not display if background data is enabled or not.

I have not found any links on how to detect if packet data is enabled or not.

Community
  • 1
  • 1
Theis Borg
  • 378
  • 4
  • 16

1 Answers1

10

I had this exact same question and I had to start a bounty to get the answer. Cost me a third of my reputation, but well worth it.

boolean mobileDataEnabled = false; // Assume disabled
        ConnectivityManager cm1 = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        try {
            Class cmClass = Class.forName(cm1.getClass().getName());
            Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
            method.setAccessible(true); // Make the method callable
            // get the setting for "mobile data"
            mobileDataEnabled = (Boolean)method.invoke(cm1);
        } catch (Exception e) {
            // Some problem accessible private API
            // TODO do whatever error handling you want here
        }

As I say, I cant claim credit for know this, my question was answered by https://stackoverflow.com/users/769265/david-wasser

but it cost me, so if you want to accept this as an answer I can start to get me some of my reputation points back! :)

Community
  • 1
  • 1
Kevin Bradshaw
  • 6,327
  • 13
  • 55
  • 78
  • Thanks! That answered one part of my question. In the meantime I have upgraded to Jelly Bean and here google has removed the background data from the sync menu :-/ Anyway - I accepted your answer since it solves my problem on JB. – Theis Borg Oct 14 '12 at 07:41
  • ahh, right, I did not realise that packet data and sync data were different settings. I presumed it was two different ways of saying the same thing. It seems like it may be possible to check this using the same method as above. I'll check it out and get back to you if I work it out! – Kevin Bradshaw Oct 14 '12 at 11:13
  • I came across this answer which seems to answer the other part of your question: http://stackoverflow.com/questions/8996327/how-to-enable-the-background-data-settings-in-android – Kevin Bradshaw Oct 15 '12 at 11:56
  • Thanks Kevin. Weird I did not find that when I searched :-) – Theis Borg Oct 15 '12 at 12:03