android 在程序中开启GPS功能

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:权限都有已经有说明的
<uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" ></uses-permission>
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;
    }


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值