GPS开关判断,强制打开关闭GPS(不用系统权限)

来源:互联网 发布:mac更新后备忘录 编辑:程序博客网 时间:2024/06/16 07:48



//需要添加的权限

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />  

<uses-permission android:name="android.permission.INTERNET" />



/** 

 * 判断GPS是否开启,GPS或者AGPS开启一个就认为是开启的 
 * @param context 
 * @return true 表示开启 
 */  
    public static boolean isOPen(final Context context) {  
        LocationManager locationManager   
                                 = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);  
        // 通过GPS卫星定位,定位级别可以精确到街(通过24颗卫星定位,在室外和空旷的地方定位准确、速度快)  
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);  
        // 通过WLAN或移动网络(3G/2G)确定的位置(也称作AGPS,辅助GPS定位。主要用于在室内或遮盖物(建筑群或茂密的深林等)密集的地方定位)  
        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);  
        if (gps || network) {  
            return true;  
        }  
        return false;  

    } 


/** 
     * 强制打开GPS 
     * @param context 
     */  
    public static void openGPS(Context context) {  
        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(context, 0, GPSIntent, 0).send();  
        } catch (CanceledException e) {  
            e.printStackTrace();  
        }  
    }  


/** 
     * 强制关闭GPS 
     * @param context 
     */  
    public static void closeGPS(Context context) {  
        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(context, 0, GPSIntent, 1).send();  
        } catch (CanceledException e) {  
            e.printStackTrace();  
        }  
    }  


原创粉丝点击