Android判斷定位功能是否可用的方法
更新時間:2018年07月31日 10:05:29 作者:brave_shine
今天小編就為大家分享一篇Android判斷定位功能是否可用的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
定位功能是否可用由定位服務和定位權限共同決定:
判斷定位服務:
/** * 手機是否開啟位置服務,如果沒有開啟那么所有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; }
判斷定位權限:
/** * 檢查權限列表 * * @param context * @param op 這個值被hide了,去AppOpsManager類源碼找,如位置權限 AppOpsManager.OP_GPS==2 * @param opString 如判斷定位權限 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; }
調用時先檢查權限:
/** * 檢查定位服務、權限 */ private void checkLocationPermission() { if (!AppUtil.isLocServiceEnable(this)) {//檢測是否開啟定位服務 if (netErrorDialog == null || !netErrorDialog.isShowing()) { locErrorDialog = DialogUtil.showLocErrorDialog(activity, 0); } } else {//檢測用戶是否將當前應用的定位權限拒絕 int checkResult = AppUtil.checkOp(this, 2, AppOpsManager.OPSTR_FINE_LOCATION);//其中2代表AppOpsManager.OP_GPS,如果要判斷懸浮框權限,第二個參數(shù)需換成24即AppOpsManager。OP_SYSTEM_ALERT_WINDOW及,第三個參數(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); } } } }
如果不能使用,彈出對話框,根據(jù)1或2,判斷跳轉頁面:
/** * 無法定位對話框 * * @param activity 上下文 * @param state 權限狀態(tài)0,未開啟服務 1,未開啟權限 * @return 對話框 */ 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) { //定位服務頁面 intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS); } else { //應用詳情頁面 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) { //如果頁面無法打開,進入設置頁面 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判斷定位功能是否可用的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
android自定義toast(widget開發(fā))示例
這篇文章主要介紹了android自定義toast(widget開發(fā))示例,需要的朋友可以參考下2014-03-03android項目實現(xiàn)帶進度條的系統(tǒng)通知欄消息
本篇文章主要介紹了android項目實現(xiàn)帶進度條的系統(tǒng)通知欄消息,就是實現(xiàn)在通知欄看到下載進度。具有一定的參考價值,感興趣的小伙伴們可以參考一下。2016-10-10Android自定義ImageView實現(xiàn)圓角功能
這篇文章主要為大家詳細介紹了Android自定義ImageView實現(xiàn)圓角功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12