亲测一下方法有用,4.2以后都可以用

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 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;        }    }



更多相关文章

  1. android 根据SD卡中图片路径读取并显示SD中的图片――源代码
  2. Android(安卓)代码里设置ImageView的src和background
  3. Android(安卓)实现序列化:Parcelable和Serializable接口的用法
  4. android 使用Java自带的HttpURLConnection 连接网络 读取返回数
  5. Android文件保存和读取
  6. android 1.6全部的权限介绍
  7. Android中使用PULL方式解析XML文件深入介绍
  8. android 三级联动开源框架用法
  9. android 项目串口通信(serialPort)开发遇到的炕

随机推荐

  1. something about nativeactivity
  2. Android(安卓)小技巧之自定义 Dialog
  3. 保存数据到手机内存代码优化(QQ登录保存
  4. Android程序——新浪微博(Listview使用)
  5. MediaRecorder和MediaPlayer的简单使用
  6. Android(安卓)for X86
  7. AsyncTask 源码解析
  8. Ubuntu 14.04 编译Oneplus One Bacon CM1
  9. Android使用线程做开始暂停
  10. file system