爱问知识人 爱问教育 医院库

如何编程实现开启或关闭gps

首页

如何编程实现开启或关闭gps


        

提交回答

全部答案

    2018-04-27 09:46:03
  •   在Activity中添加如下方法:
      [java] view
    plaincopyprint?
      private void toggleGPS() {
      Intent gpsIntent = new Intent();
      gpsIntent。
      setClassName("com。android。settings",
      "com。android。settings。widget。SettingsAppWidgetProvider");
      gpsIntent。
      addCategory("android。intent。category。ALTERNATIVE");
      gpsIntent。setData(Uri。parse("custom:3"));
      try {
      PendingIntent。
      getBroadcast(this, 0, gpsIntent, 0)。send();
      }
      catch (CanceledException e) {
      e。printStackTrace();
      }
      }
      private void toggleGPS() {
    Intent gpsIntent = new Intent();
    gpsIntent。
      setClassName("com。android。settings",
    "com。android。settings。widget。SettingsAppWidgetProvider");
    gpsIntent。
      addCategory("android。intent。category。ALTERNATIVE");
    gpsIntent。setData(Uri。parse("custom:3"));
    try {
    PendingIntent。
      getBroadcast(this, 0, gpsIntent, 0)。send();
    }
    catch (CanceledException e) {
    e。printStackTrace();
    }
    }
      这个方法是1个纯开关,如果当前是开启的,那么就会关闭它,反之亦然。
      
      检查GPS开关状态
      那么,如何查看当前的GPS开关状态呢?可以用上面提到的反射方式调用isLocationProviderEnabled,代码片断如下:
      [java] view
    plaincopyprint?
      secureClass = cl。
      loadClass("android。provider。Settings$Secure");
      isMethod = secureClass。getMethod("isLocationProviderEnabled",
      ContentResolver。
      class, String。class);
      Boolean ret = (Boolean) isMethod。invoke(secureClass, this
      。getContentResolver(), "gps");
      secureClass = cl。
      loadClass("android。provider。Settings$Secure");
    isMethod = secureClass。getMethod("isLocationProviderEnabled",
    ContentResolver。
      class, String。class);
    Boolean ret = (Boolean) isMethod。invoke(secureClass, this
    。getContentResolver(), "gps");
      也可以直接用下面的方法:
      [java] view
    plaincopyprint?
      private void isGPSEnable() {
      /* 用Setting。
      System来读取也可以,只是这是更旧的用法
      String str = Settings。System。getString(getContentResolver(),
      Settings。Secure。
      LOCATION_PROVIDERS_ALLOWED);
      */
      String str = Settings。Secure。getString(getContentResolver(),
      Settings。
      Secure。LOCATION_PROVIDERS_ALLOWED);
      Log。v("GPS", str);
      if (str != null) {
      return str。
      contains("gps");
      }
      else{
      return false;
      }
      }
      private void isGPSEnable() {
    /* 用Setting。
      System来读取也可以,只是这是更旧的用法
    String str = Settings。System。getString(getContentResolver(),
    Settings。Secure。
      LOCATION_PROVIDERS_ALLOWED);
    */
    String str = Settings。Secure。getString(getContentResolver(),
    Settings。
      Secure。LOCATION_PROVIDERS_ALLOWED);
    Log。v("GPS", str);
    if (str != null) {
    return str。
      contains("gps");
    }
    else{
    return false;
    }
    }
      这2种方法的原理都是一样的,方法2其实也就是isLocationProviderEnabled实际调用的代码,只是Google未对读取操作进行权限限制。
      

    刘***

    2018-04-27 09:46:03

类似问题

换一换

相关推荐

正在加载...
最新问答 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 170-189
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):