Android判斷定位功能是否可用的方法
定位功能是否可用由定位服務(wù)和定位權(quán)限共同決定:
判斷定位服務(wù):
/** * 手機(jī)是否開(kāi)啟位置服務(wù),如果沒(méi)有開(kāi)啟那么所有app將不能使用定位功能 */ public static boolean isLocServiceEnable(Context context) { LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (gps || network) { return true; } return false; }
判斷定位權(quán)限:
/** * 檢查權(quán)限列表 * * @param context * @param op 這個(gè)值被hide了,去AppOpsManager類源碼找,如位置權(quán)限 AppOpsManager.OP_GPS==2 * @param opString 如判斷定位權(quán)限 AppOpsManager.OPSTR_FINE_LOCATION * @return @see 如果返回值 AppOpsManagerCompat.MODE_IGNORED 表示被禁用了 */ public static int checkOp(Context context, int op, String opString) { final int version = Build.VERSION.SDK_INT; if (version >= 19) { Object object = context.getSystemService(Context.APP_OPS_SERVICE); // Object object = context.getSystemService("appops"); Class c = object.getClass(); try { Class[] cArg = new Class[3]; cArg[0] = int.class; cArg[1] = int.class; cArg[2] = String.class; Method lMethod = c.getDeclaredMethod("checkOp", cArg); return (Integer) lMethod.invoke(object, op, Binder.getCallingUid(), context.getPackageName()); } catch (Exception e) { e.printStackTrace(); if (Build.VERSION.SDK_INT >= 23) { return AppOpsManagerCompat.noteOp(context, opString, context.getApplicationInfo().uid, context.getPackageName()); } } } return -1; }
調(diào)用時(shí)先檢查權(quán)限:
/** * 檢查定位服務(wù)、權(quán)限 */ private void checkLocationPermission() { if (!AppUtil.isLocServiceEnable(this)) {//檢測(cè)是否開(kāi)啟定位服務(wù) if (netErrorDialog == null || !netErrorDialog.isShowing()) { locErrorDialog = DialogUtil.showLocErrorDialog(activity, 0); } } else {//檢測(cè)用戶是否將當(dāng)前應(yīng)用的定位權(quán)限拒絕 int checkResult = AppUtil.checkOp(this, 2, AppOpsManager.OPSTR_FINE_LOCATION);//其中2代表AppOpsManager.OP_GPS,如果要判斷懸浮框權(quán)限,第二個(gè)參數(shù)需換成24即AppOpsManager。OP_SYSTEM_ALERT_WINDOW及,第三個(gè)參數(shù)需要換成AppOpsManager.OPSTR_SYSTEM_ALERT_WINDOW int checkResult2 = AppUtil.checkOp(this, 1, AppOpsManager.OPSTR_FINE_LOCATION); if (AppOpsManagerCompat.MODE_IGNORED == checkResult || AppOpsManagerCompat.MODE_IGNORED == checkResult2) { if (netErrorDialog == null || !netErrorDialog.isShowing()) { locErrorDialog = DialogUtil.showLocErrorDialog(activity, 1); } } } }
如果不能使用,彈出對(duì)話框,根據(jù)1或2,判斷跳轉(zhuǎn)頁(yè)面:
/** * 無(wú)法定位對(duì)話框 * * @param activity 上下文 * @param state 權(quán)限狀態(tài)0,未開(kāi)啟服務(wù) 1,未開(kāi)啟權(quán)限 * @return 對(duì)話框 */ public static Dialog showLocErrorDialog(Activity activity, int state) { Dialog locErrorDialog = new Dialog(activity, R.style.MyDialog); View contentView = View.inflate(activity, R.layout.dialog_tip_error_loc, null); locErrorDialog.setContentView(contentView); locErrorDialog.setCanceledOnTouchOutside(true); locErrorDialog.show(); TextView checkNetCancel = contentView.findViewById(R.id.tv_submit_no); TextView checkNet = contentView.findViewById(R.id.tv_submit_yes); checkNetCancel.setOnClickListener(view -> { locErrorDialog.dismiss(); }); checkNet.setOnClickListener(view -> { locErrorDialog.dismiss(); Intent intent = new Intent(); if (state == 0) { //定位服務(wù)頁(yè)面 intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS); } else { //應(yīng)用詳情頁(yè)面 intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.setData(Uri.parse("package:" + activity.getPackageName())); } intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { activity.startActivity(intent); } catch (ActivityNotFoundException ex) { //如果頁(yè)面無(wú)法打開(kāi),進(jìn)入設(shè)置頁(yè)面 intent.setAction(Settings.ACTION_SETTINGS); try { activity.startActivity(intent); } catch (Exception e) { e.printStackTrace(); } } }); return locErrorDialog; }
Dialog樣式:
<style name="MyDialog" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowNoTitle">true</item> <item name="android:background">@color/transparent</item> </style>
以上這篇Android判斷定位功能是否可用的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
android自定義toast(widget開(kāi)發(fā))示例
這篇文章主要介紹了android自定義toast(widget開(kāi)發(fā))示例,需要的朋友可以參考下2014-03-03android項(xiàng)目實(shí)現(xiàn)帶進(jìn)度條的系統(tǒng)通知欄消息
本篇文章主要介紹了android項(xiàng)目實(shí)現(xiàn)帶進(jìn)度條的系統(tǒng)通知欄消息,就是實(shí)現(xiàn)在通知欄看到下載進(jìn)度。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-10-10Android Studio的安裝及第一次啟動(dòng)時(shí)的配置問(wèn)題
這篇文章主要介紹了Android Studio的安裝及第一次啟動(dòng)時(shí)的配置,需要的朋友可以參考下2019-09-09Android自定義ImageView實(shí)現(xiàn)圓角功能
這篇文章主要為大家詳細(xì)介紹了Android自定義ImageView實(shí)現(xiàn)圓角功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Android實(shí)現(xiàn)拍照截取和相冊(cè)圖片截取
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)拍照截取和相冊(cè)獲取圖片截取,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12