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.