博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
打开GPS
阅读量:6907 次
发布时间:2019-06-27

本文共 1536 字,大约阅读时间需要 5 分钟。

hot3.png

打开GPS

1.第一种方法
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(StartActivity.this, 0, gpsIntent, 0).send();
             } catch (CanceledException e) {
                     e.printStackTrace();
             }
     }
  
2.第二种方法
   private void openGPSSettings() {       
       //获取GPS现在的状态(打开或是关闭状态)
    boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled( getContentResolver(), LocationManager.GPS_PROVIDER );
  
     if(gpsEnabled)
     {
    //关闭GPS
      Settings.Secure.setLocationProviderEnabled( getContentResolver(), LocationManager.GPS_PROVIDER, false );
     }
     else
     {
      //打开GPS  www.2cto.com
      Settings.Secure.setLocationProviderEnabled( getContentResolver(), LocationManager.GPS_PROVIDER, true);
    }
    
  3.第三种方法(手动设置)
     LocationManager alm = (LocationManager)StartActivity.this.getSystemService(Context.LOCATION_SERVICE);       
        if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))
        {           
         Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();
        }       
       
        Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
        startActivityForResult(intent,0); //此为设置完成后返回到获取界面  
    
  第一第二种需要加上权限
  <!--允许程序读取或写入系统设置 -->
  <uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>
  <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>

转载于:https://my.oschina.net/u/1035715/blog/148211

你可能感兴趣的文章
递推公式-HDOJ2041
查看>>
android中listview的getView方法不调用的一种情况
查看>>
HTTP socket 类
查看>>
两个有用的宏:“禁止类成员复制”以及“禁止隐式构造”
查看>>
如何创建OpenCart主题/模板
查看>>
DES加密解密 JAVA与.NET互通程序代码
查看>>
springframework shedular task
查看>>
《分布式服务框架原理与实践》——第5章协议栈阅读笔记
查看>>
Server-Sent Events
查看>>
nio服务器端
查看>>
Android 在App和Java Web开发中的使用J2V8
查看>>
Ubuntu下打包解压命令
查看>>
事件冒泡与捕获详解
查看>>
art-template4.0使用
查看>>
jsp中使用EL获得contextPath
查看>>
SOFARPC 5.5.X 新版发布 | 集成 Nacos 与 Hystrix
查看>>
java String compareTo
查看>>
详解openstack命令启动实现流程及原理(nova --debug image-list)
查看>>
Java-NIO-Buffer详解
查看>>
elasticsearch elk最全java api 搜索 聚合、嵌套查询
查看>>