【问题标题】:android turn GPS On & Off Programmatically in 2.2?android 在 2.2 中以编程方式打开和关闭 GPS?
【发布时间】:2011-12-22 07:39:55
【问题描述】:

我在我的应用程序中使用 GPS,我想以编程方式打开和关闭 GPS 以节省电量我该怎么做 :(

这是为了关机,我需要开机

private void turnGPSOnOff(){
  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);
    //Toast.makeText(this, "Your GPS is Enabled",Toast.LENGTH_SHORT).show();
  }
}

【问题讨论】:

标签: android google-maps-api-3 gps


【解决方案1】:
private void turnGPSOnOn(){
  String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
  if(provider.contains("gps")){ // for turn on
    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);
    Toast.makeText(this, "Your GPS is Enabled",Toast.LENGTH_SHORT).show();
  }
}

【讨论】:

    【解决方案2】:
        try
                    {
                    dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
                  } 
                  catch (SecurityException e1) 
                  {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                  } 
                  catch (NoSuchMethodException e1) 
                  {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                  }
    
                    dataMtd.setAccessible(true); 
                   try {
    
                    dataMtd.invoke(conm,true);
    
    
                    //gprDisable();
                  } 
                   catch (IllegalArgumentException e) 
                   {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                  } 
                   catch (IllegalAccessException e) 
                   {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                  } 
                   catch (InvocationTargetException e) 
                   {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                  }
    

    如果您将选项设置为 false,您可以禁用 GPS。希望这对您有所帮助。

    【讨论】:

      【解决方案3】:

      你可以打开GPS使用

      LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
      LocationListener locationListener = new CTLocationListener();
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1.0f, locationListener);
      

      并使用关闭它

      locationManager.removeUpdates(locationListener);
      

      或者您可能还会在 gps 的使用中找到另一个线程: How can I enable or disable the GPS programmatically on Android?

      【讨论】:

      • 这不会关闭 Gps,它会请求您的应用接收 gps 更新,然后请求您的应用不再接收更新。
      猜你喜欢
      • 1970-01-01
      • 2015-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-02
      相关资源
      最近更新 更多