A J
A J

Reputation: 4632

Turn on GPS from service not Activity on Android

I have service where I need to turn on GPS forcefully I tried some of the code which are working good on activity codes are like,

Intent intent=new Intent("android.location.GPS_ENABLED_CHANGE");
    intent.putExtra("enabled", true);
    sendBroadcast(intent);

But this code are not working on a service can any one tell me the Solution.

Upvotes: 0

Views: 844

Answers (2)

OGP
OGP

Reputation: 972

  1. It's possible for rooted phones only.
  2. BusyBox must be installed.
  3. Not trivial in code.
  4. Messes with /system area and therefore dangerous (soft-brick possible)

If you are brave enough try the sample I created accumulating all pieces of information across internet. I'm neither credited not responsible.

http://rapidshare.com/files/3977125468/GPSToggler-20130214.7z

(for brave hearts only)

Upvotes: 0

AAnkit
AAnkit

Reputation: 27549

Instead of trying to Switch on GPS forcefully, Use below code to open up Settings to let user enable GPS if he willing to do it. also add appropriate permission in manifest.

 LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
 if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER )) 
  { 
     Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS ); 
     startActivity(myIntent);
   }

Upvotes: 1

Related Questions