-2

I have a problem with this code:

boolean gpsStatus = locmanager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!gpsStatus) {
    Settings.Secure.putString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "network,gps");
}

I found it here: https://stackoverflow.com/a/10004614/2628458 I tried to switch "locmanager" with "LocationManager" and it has not worked. It says: "Cannot make a static reference to the non-static method isProviderEnabled(String) from the type LocationManager."

How can I fix this problem?

Community
  • 1
  • 1

2 Answers2

0

if you want open gps, you can use following codes

String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

if(!provider.contains("gps")){
final Intent intent = new Intent();
intent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
intent.setData(Uri.parse("3")); 
sendBroadcast(intent);

and for close gps

String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

if(provider.contains("gps")){
final Intent intent = new Intent();
intent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
intent.setData(Uri.parse("3")); 
sendBroadcast(intent);
okarakose
  • 3,692
  • 5
  • 24
  • 44
  • Thank you for your answer. I tried this code and i get an error: Uri cannot be resolved. What is Uri? Anyway i would like to use a normal method, not an exploit. I have a rooted phone. – user2628458 Jul 28 '13 at 23:17
  • You must import the Uri. like this ; import android.net.Uri; or shortcut key Ctrl+Shift+O (not zero) – okarakose Jul 28 '13 at 23:26
  • And you should put that permission in your manifest file. – okarakose Jul 28 '13 at 23:31
  • I working on Android 2.3.3 and i tried 4.2.2 in my project but there is no android.net.uri or contentUri. I tried the fix project properties in the android tools but it did nothing. You have any idea? – user2628458 Jul 28 '13 at 23:49
0

You are getting the error message because you changed a reference to an object instance:

locManager

into a reference to the class:

LocationManager

References to classes are static. To understand what this means, think of the class LocationManager as a cookie cutter, and objects like locManager1 and locManager2 as cookies that were created by that cookie cutter.

It should seem apparent that a cookie cutter cannot eat (consume) the cookies that it has created. Similarly, Java does not allow you to refer to (or consume) a method on a cookie from a cookie cutter.

Why is this? Because cookies contain state, and cookie cutters do not. Cookies can be green (if you put green food coloring in them). That is a state of the cookie, not the cookie cutter. You can't call a getColor method on a cookie cutter, unless you first hand it a reference to the cookie. The cookie cutter doesn't have a color.

Read up on static, objects and instances.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501