1. LocationManager locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);       2.         3.        boolean isEnabled= locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);  4.        if(!isEnabled){  5.            //这段代码可以实现GPS开关状态的切换   6.                Intent GPSIntent = new Intent();  7.                GPSIntent.setClassName("com.android.settings",  8.                        "com.android.settings.widget.SettingsAppWidgetProvider");  9.                GPSIntent.addCategory("android.intent.category.ALTERNATIVE");  10.                GPSIntent.setData(Uri.parse("custom:3"));  11.                try {  12.                    PendingIntent.getBroadcast(MainActivity.this, 0, GPSIntent, 0).send();  13.                } catch (CanceledException e) {  14.                    e.printStackTrace();  15.                }      16.//              Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, true);  注释掉的代码,表面上看可以简单的实现开启GPS功能,但是运行后发现后没有权限,即使加了权限依旧提示permission denied PS:权限都有已经有说明的Android 在程序中开启网络功能/**    * 移动网络开关    */       public Object invokeBooleanArgMethod(String methodName,               boolean value) throws Exception {       mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);           Class ownerClass = mConnectivityManager.getClass();           Class[]  argsClass = new Class[1];               argsClass[0] = boolean.class;           Method method = ownerClass.getMethod(methodName,argsClass);           return method.invoke(mConnectivityManager, value);       }    //判断移动网络    public boolean isNetworkConnected(Context context) {            if (context != null) {                ConnectivityManager mConnectivityManager = (ConnectivityManager) context                        .getSystemService(Context.CONNECTIVITY_SERVICE);                NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();                if (mNetworkInfo != null) {                   return mNetworkInfo.isAvailable();                }           }           return false;    }

更多相关文章

  1. Android(安卓)Socket网络通信
  2. android 飞行模式 注册广播后 三种状态监听
  3. android 检查gps
  4. android 键盘关闭/显示
  5. Android(安卓)实现开关灯效果
  6. android判断网络状态
  7. Android配置ip地址
  8. android之Switch开关
  9. android TelephonyManager

随机推荐

  1. android intent使用方法
  2. Android(安卓)网络请求详解
  3. android四大组件之Broadcast(广播)使用详
  4. Android(安卓)launchMode的使用
  5. Android(安卓)持续集成实践(一)——从0开始
  6. Android——消息机制中的Message Pool是
  7. android UI进阶之布局的优化
  8. Android(安卓)引入FFmpeg库so文件
  9. Android WebView 访问https显示空白页
  10. Android Fragment重叠问题