I am creating an anti-theft application and, locating my phone through sms and it works perfectly until 2.3. But in 4.0 I can't turn on or off gps programmatically is there any other possible way to switch on gps through code.
            Asked
            
        
        
            Active
            
        
            Viewed 8.6k times
        
    30
            
            
        - 
                    http://stackoverflow.com/questions/4721449/enable-gps-programatically-like-tasker have look here – Dilip Mar 15 '13 at 06:31
- 
                    2past pic of code in your question . – dharmendra Apr 10 '13 at 06:06
- 
                    Have you checked this link http://stackoverflow.com/questions/4721449/enable-gps-programatically-like-tasker ? – Jitendra Apr 10 '13 at 06:08
1 Answers
40
            Try using this code. It worked for me on all the versions.
public void turnGPSOn()
{
     Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
     intent.putExtra("enabled", true);
     this.ctx.sendBroadcast(intent);
    String provider = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if(!provider.contains("gps")){ //if gps is disabled
        final Intent poke = new Intent();
        poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
        poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
        poke.setData(Uri.parse("3")); 
        this.ctx.sendBroadcast(poke);
    }
}
// automatic turn off the gps
public void turnGPSOff()
{
    String provider = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if(provider.contains("gps")){ //if gps is enabled
        final Intent poke = new Intent();
        poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
        poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
        poke.setData(Uri.parse("3")); 
        this.ctx.sendBroadcast(poke);
    }
}
 
    
    
        Sagar Patil
        
- 1,400
- 15
- 29
- 
                    this code turns on gps in my device and it is not working in turn off i am using 4.0.4 ics – Gopi.cs Mar 15 '13 at 09:11
- 
                    2hi it is not working in samsung galaxy y. it put gps receiver is on always on mode, after getting fix it should be on sleep mode.. – Sandeep Jun 02 '13 at 10:00
- 
                    4try this code to turn off gps Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE"); intent.putExtra("enabled", false); sendBroadcast(intent); – Sagar Patil Jun 03 '13 at 07:21
- 
                    3For those who are facing problem with "ctx". ctx is nothing but instance of the activity. If you declare these functions in your activity then use "className.this" instead of ctx. – Sagar Patil Nov 29 '13 at 06:41
- 
                    Thanks..... But i think it wont work if the widget is not present.. And i guess the widget is only available on Nexus Devices,... – Shridutt Kothari Jan 28 '14 at 08:01
- 
                    4I am getting "java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.location.GPS_ENABLED_CHANGE". App is crashing – user1035292 Jun 05 '15 at 06:15
- 
                    I am also getting "java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.location.GPS_ENABLED_CHANGE". - Any Advice – Thiago Dec 04 '15 at 04:03
- 
                    Joolah and user1035292 you have to add the permissions to the manifest. – Diogo Nunes Jan 06 '16 at 15:50
- 
                    
- 
                    @ Diogo Nunes-- which permission we need to add. facing same crash like Joolah and user1035292 – Abhi Feb 16 '16 at 07:31
- 
                    
- 
                    
- 
                    10
- 
                    1
 
    