Start/Stop GPS Programmatically in Android

private void setGPSOn()
{
    String provider = Settings.Secure.getString(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")); 
        sendBroadcast(poke);
    }
}

private void setGPSOff(){
    String provider = Settings.Secure.getString(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")); 
        sendBroadcast(poke);
    }
}
Advertisements

7 thoughts on “Start/Stop GPS Programmatically in Android

  1. Nauru March 13, 2013 at 2:55 am Reply

    Thank you for sharing excellent informations. Your website is very cool. I’m impressed by the details that youˇ¦ve on this website. It reveals how nicely you understand this subject. Bookmarked this website page, will come back for extra articles. You, my friend, ROCK! I found just the information I already searched everywhere and just could not come across. What a perfect website.

  2. kwiaciarnia online March 31, 2013 at 7:26 pm Reply

    I love your blog.. very nice colors & theme. Did you design this website yourself or did you hire someone to do it for you? Plz answer back as I’m looking to construct my own blog and would like to know where u got this from. appreciate it

    • Rajeswara rao April 1, 2013 at 11:45 am Reply

      Thank you
      its free Design Theme and its updated me only

  3. Anonymous April 3, 2013 at 7:16 am Reply

    Hi, Neat post. There’s a problem with your website in internet explorer, would test this… IE still is the market leader and a big portion of people will miss your excellent writing due to this problem.

  4. torty weselne katowice April 6, 2013 at 5:32 am Reply

    What’s Taking place i am new to this, I stumbled upon this I’ve found It absolutely helpful and it has aided me out loads. I hope to give a contribution & help different customers like its aided me. Good job.

  5. website April 6, 2013 at 10:05 pm Reply

    I like the helpful information you provide in your articles. I’ll bookmark your weblog and check again here frequently. I’m quite sure I’ll learn many new stuff right here! Best of luck for the next!

  6. rejestracja firmy April 6, 2013 at 10:11 pm Reply

    Hello my friend! I wish to say that this post is awesome, nice written and include approximately all significant infos. I would like to see more posts like this.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: