微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我如何以编程方式启用和禁用GPS

如何解决我如何以编程方式启用和禁用GPS

| 我正在开发一个应用程序,其中在我开始活动时要启用GPS,在退出应用程序时要禁用GPS。我可以通过我的程序来做吗。 任何帮助,将不胜感激。     

解决方法

这是代码...
 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);  
   }  }    
    ,您可以使用来启动GPS
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    LocationListener locationListener = new CTLocationListener();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1.0f,locationListener);
停止使用gps提供程序
locationManager.removeUpdates(locationListener);
    ,我在所有网站上都对此主题进行了研究。我发现下面的代码我尝试了它的工作。 方法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);
方法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);
        }
    }
有关更多信息,您可以访问以下网站:http://androidmaterial.blogspot.com/2012/04/how-to-enabledisable-gps-in-android.html     ,这就是我能找到的全部(我的代码未在活动中运行,因此我有一个ApplicationContext类,我将其称为静态方法getInstance。您不必在活动中执行此操作。对不起,代码格式设置) :
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();
            }
        }
    }
    ,要通过您的代码禁用GPS,
    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
    ,如果我没记错的话,你不可能完全以编程方式做到这一点。最好是激发意图以启动设置页面,但随后用户必须在设置页面中启用gps设置。 没有用户干预,您将无法在后台执行此操作。     ,我知道为时已晚,但是直接启动定位服务将无法工作。而是使用以下代码启动定位服务。
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
    ,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);
        }
    }
}
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。