0

Okay this is driving me nuts! Please consider the below code:

public class PhoneConfigService extends Activity 
{
    public int CheckAPIVersion()
    {
        int version = Build.VERSION.SDK_INT;
        return version;
    }

    //Basically I am trying to check if data saver mode if enabled for devices with API level greater than 
    @TargetApi(24)
    public boolean CheckIfAllowDataSaverModeEnabledForApp() { //line number 70
        ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        if (CheckAPIVersion() < 24) {
            return false;
        } else {
            switch (connMgr.getRestrictBackgroundStatus()) {
                case RESTRICT_BACKGROUND_STATUS_ENABLED:
                    // Background data usage and push notifications are enabled which will block or limit the usage of this app
                    return true;

                case RESTRICT_BACKGROUND_STATUS_WHITELISTED:
                case RESTRICT_BACKGROUND_STATUS_DISABLED:
                    // Data Saver is disabled or the app is whitelisted
            }
        }
        return false;
    }
}

//My main class
public class TestChanges extends Activity
{
    @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.test_changes);

           String dataSaverMode = Boolean.toString(inst.CheckIfAllowDataSaverModeEnabledForApp());
           TextView tvSummary = (TextView) findViewById(R.id.summary);

           if(dataSaverMode == "true")
           {
              //Do some logic
           }
}

But whenever I put a break and stepping into the CheckIfAllowDataSaverModeEnabledForApp() function, I keep on getting an:

There is no executable code at PhoneConfigService.java:70

Even after I have added the following to my app's build.gradle file:

debug { debuggable true minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt') signingConfig signingConfigs.release }

Still does not make a difference. I have checked the build variants and I am running the app in debug mode.

UPDATE: Please note that it throws the error before it even enters the CheckIfAllowDataSaverModeEnabledForApp() block. It can't even get to the first brace '{' inside the method.

Update II: As per @MC Emperor's comment, I removed or rather moved the breakpoint to a point further down within the method and I am getting a different exception now:

java.lang.IllegalStateException: System services not available to Activities before onCreate()

After some reading, I see that it might be caused in my onCreate() method in my TestChanges class where I am instantiating the PhoneConfigService class viz.

PhoneConfigService inst = new PhoneConfigService();. How can I get I around this? Apparently I can't use new to instantiate the class.

I disabled Instant Run, rebuilt (and cleaned) the project and same result.

Harold_Finch
  • 682
  • 2
  • 12
  • 33
  • is line 70 inside your case statement, by chance? Why not just have a `return connMgr.getRestrictBackgroundStatus() == RESTRICT_BACKGROUND_STATUS_ENABLED` statement there? – corn3lius Sep 21 '17 at 13:58
  • It throws the error as it tries to enter the function. CheckIfAllowDataSaverModeEnabledForApp() – Harold_Finch Sep 21 '17 at 13:59
  • I don't know, but keep this in mind: 1. `dataSaverMode == "true"` — **never** compare strings like this. Always use the `equals()` method. 2. You should stick to the Java Naming Conventions: method names always start with a lowercase letter. – MC Emperor Sep 21 '17 at 13:59
  • @MC Emperor What does code standards have to do with the error at hand? There are several other functions in the `PhoneConfigService` class which I am able to debug and step into without any hassle. And those methods follow the same naming convention which I have used. – Harold_Finch Sep 21 '17 at 14:02
  • @corn3lius I tried your implementation and I still get the same error. I updated my question – Harold_Finch Sep 21 '17 at 14:06
  • Also why the double parenthesis in the annotation ? shouldn't it be - `@TargetApi(24)` – corn3lius Sep 21 '17 at 14:09
  • @Harold_Finch Not much ... except that it is not only a good habit, it also makes your code better readable. For instance, see how the method names are highlighted as if they were class names. – MC Emperor Sep 21 '17 at 14:10
  • What happens if you move the break point? – MC Emperor Sep 21 '17 at 14:11
  • @corn3lius I fixed that and I still get that error.. – Harold_Finch Sep 21 '17 at 14:13
  • @MC Emperor Please attempt to reproduce the problem with your coding standards and see if the error will go away. – Harold_Finch Sep 21 '17 at 14:14
  • Try disabling instant run, do a rebuild and try again – Veneet Reddy Sep 21 '17 at 14:15

1 Answers1

0

I referred to @LabeebPanampullan 's answer posted here: How can I use getSystemService in a non-activity class (LocationManager)?

  1. I added a constructor (as shown below) to my PhoneConfigService class and removed the extends Activity This was the root cause to my problem. I knew that because of @Raghunandan 's answer posted here: IllegalStateException "System services not available to Activities before onCreate()"

Context mContext; public PhoneConfigService(Context mContext) { this.mContext = mContext; }

  1. In my CheckIfAllowDataSaverModeEnabledForApp() method I passed Context as a parameter and changed this line from: ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); to: ConnectivityManager connMgr = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);

  2. In my TestChanges class, I passed in the context when instantiating the PhoneConfigService class to look like this: PhoneConfigService inst = new PhoneConfigService(TestChanges.this); and all is working as expected now. Thanks to everyone for their valued input.

Harold_Finch
  • 682
  • 2
  • 12
  • 33