How can i enable and disable GPS programatically

2020-07-23 08:50发布

I am developing an application wherein I want to enable GPS, when I start my activity and I want to disable GPS when I exit my application. Can I do it through my program..

Any help would be appreciated.

标签: android gps
8条回答
戒情不戒烟
2楼-- · 2020-07-23 09:15

Here is all I can find (my code doesn't run in an activity so I have an ApplicationContext class that I call a static method getInstance. You don't have to do this in an activity. Sorry for the code formatting):

public static boolean isGPSEnabled(){
        LocationManager locationManager = (LocationManager)ApplicationContext.getInstance().getSystemService(Context.LOCATION_SERVICE);
        boolean enabled = locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER );
        if(enabled) return enabled;
        String provider = Settings.Secure.getString(ApplicationContext.getInstance().getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        return provider.contains("gps");
    }

private static boolean canToggleGPSDirectly() {
        PackageManager pacman = ApplicationContext.getInstance().getPackageManager();
        PackageInfo pacInfo = null;

        try {
            pacInfo = pacman.getPackageInfo("com.android.settings", PackageManager.GET_RECEIVERS);
        } catch (NameNotFoundException e) {
            return false; //package not found
        }

        if(pacInfo != null){
            for(ActivityInfo actInfo : pacInfo.receivers){
                //test if recevier is exported. if so, we can toggle GPS.
                if(actInfo.name.equals("com.android.settings.widget.SettingsAppWidgetProvider") && actInfo.exported){
                    return true;
                }
            }
        }

        return false; //default
    }

//this only works in older versions of android (it woks on 2.2)
private static void toggleGPS(){
        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")); 
        ApplicationContext.getInstance().sendBroadcast(poke);
    }

private static void showGPSSettingsScreen(){
        //show the settings screen
        Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ApplicationContext.getInstance().startActivity(myIntent);

        // this doens't work if your app isn't signed as firmware....Settings.Secure.setLocationProviderEnabled(ApplicationContext.getInstance().getContentResolver(), LocationManager.GPS_PROVIDER, true);
    }

public static void ensureGPSStatus(boolean on){
        boolean isGPSEnabled = isGPSEnabled();
        if((isGPSEnabled && !on) || (!isGPSEnabled && on)){
            if(canToggleGPSDirectly()){
                toggleGPS();
            }else{
                showGPSSettingsScreen();
            }
        }
    }
查看更多
Ridiculous、
3楼-- · 2020-07-23 09:16

If I am not mistaken, you can't do it entirely programmatically. The best is to fire an intent to start the settings page but subsequently user must enable the gps setting in the settings page.

You can't do it in the background without user intervention.

查看更多
聊天终结者
4楼-- · 2020-07-23 09:21

I researched about this subject on all web sites. and I found this code like below. I tried its working.

Method-1

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

Method-2

 // If GPS is OFF
    private void turnGPSOn(){
        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);
        }
    }
    // If GPS is ON
    private void turnGPSOff(){
        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);
        }
    }

Fore more information you can visit this web site : http://androidmaterial.blogspot.com/2012/04/how-to-enabledisable-gps-in-android.html

查看更多
forever°为你锁心
5楼-- · 2020-07-23 09:24

GpsSettings

public class GpsSettings
{
    @SuppressWarnings("deprecation")
    public static void turnGPSOn(Context context)
    {

        Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");

        intent.putExtra("enabled", true);

        context.sendBroadcast(intent);  

        String provider = Settings.Secure.getString(context.getContentResolver(),
                Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        Settings.Secure.setLocationProviderEnabled(context.getContentResolver(), "gps", true);

        if (!provider.contains("gps"))
        {
            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"));
            context.sendBroadcast(poke);
        }
    }

    @SuppressWarnings("deprecation")
    public static void turnGPSOff(Context context)
    {

        Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");

        intent.putExtra("enabled", false);

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

        if (provider.contains("gps"))
        {
            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"));
            context.sendBroadcast(poke);
        }
    }
}
查看更多
\"骚年 ilove
6楼-- · 2020-07-23 09:26

Here is the code...

 private void turnGPSOn(){   

    String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);   
    if(!provider.contains("gps")){      
        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);  
   }  }    
查看更多
淡お忘
7楼-- · 2020-07-23 09:29

To Disable the GPS through your code,

    Intent my_intent = new Intent();
    my_intent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
    my_intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
    my_intent.setData(Uri.parse("3")); 
     sendBroadcast(my_intent);  //use context.sendBroadcast(my_intent) if you are in any Broadcast activity
查看更多
登录 后发表回答