android 代码 操作 gps 开关

来源:互联网 发布:西门子苏州研究院数据 编辑:程序博客网 时间:2024/05/16 18:32

在研究android gps 的时候,发现android系统隐藏了对gps开关操作的接口,也不知道为什么,个人觉得可能是怕所有的程序都区操作gps 开关从而导致混乱吧!但是还是在网上看到了一个用代码对gps 开关进行操作的方法。 贡献出来!

==================================================================================== 

直接上代码:

         private voidtoggleGPS() {

                 IntentgpsIntent = newIntent();

                 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 (CanceledExceptione) {

                          e.printStackTrace();

                 }

    }