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:权限都有已经有说明的


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


你可能感兴趣的:(Android,功能代码)