Android判断定位功能是否可用的方法

发布时间:2026/8/27 18:33:00
Android判断定位功能是否可用的方法 定位功能是否可用由定位服务和定位权限共同决定判断定位服务代码语言javascript/** * 手机是否开启位置服务如果没有开启那么所有app将不能使用定位功能 */publicstaticbooleanisLocServiceEnable(Context context){LocationManager locationManager(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);boolean gpslocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);boolean networklocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);if(gps||network){returntrue;}returnfalse;}判断定位权限代码语言javascript/** * 检查权限列表 * * param context * param op 这个值被hide了去AppOpsManager类源码找如位置权限 AppOpsManager.OP_GPS2 * param opString 如判断定位权限 AppOpsManager.OPSTR_FINE_LOCATION * return see 如果返回值 AppOpsManagerCompat.MODE_IGNORED 表示被禁用了 */publicstaticintcheckOp(Context context,int op,String opString){final int versionBuild.VERSION.SDK_INT;if(version19){Object objectcontext.getSystemService(Context.APP_OPS_SERVICE);// Object object context.getSystemService(appops);Class cobject.getClass();try{Class[]cArgnewClass[3];cArg[0]int.class;cArg[1]int.class;cArg[2]String.class;Method lMethodc.getDeclaredMethod(checkOp,cArg);return(Integer)lMethod.invoke(object,op,Binder.getCallingUid(),context.getPackageName());}catch(Exception e){e.printStackTrace();if(Build.VERSION.SDK_INT23){returnAppOpsManagerCompat.noteOp(context,opString,context.getApplicationInfo().uid,context.getPackageName());}}}return-1;}调用时先检查权限代码语言javascript/** * 检查定位服务、权限 */privatevoidcheckLocationPermission(){if(!AppUtil.isLocServiceEnable(this)){//检测是否开启定位服务if(netErrorDialognull||!netErrorDialog.isShowing()){locErrorDialogDialogUtil.showLocErrorDialog(activity,0);}}else{//检测用户是否将当前应用的定位权限拒绝int checkResultAppUtil.checkOp(this,2,AppOpsManager.OPSTR_FINE_LOCATION);//其中2代表AppOpsManager.OP_GPS如果要判断悬浮框权限第二个参数需换成24即AppOpsManager。OP_SYSTEM_ALERT_WINDOW及第三个参数需要换成AppOpsManager.OPSTR_SYSTEM_ALERT_WINDOWint checkResult2AppUtil.checkOp(this,1,AppOpsManager.OPSTR_FINE_LOCATION);if(AppOpsManagerCompat.MODE_IGNOREDcheckResult||AppOpsManagerCompat.MODE_IGNOREDcheckResult2){if(netErrorDialognull||!netErrorDialog.isShowing()){locErrorDialogDialogUtil.showLocErrorDialog(activity,1);}}}}如果不能使用弹出对话框根据1或2判断跳转页面代码语言javascript/** * 无法定位对话框 * * param activity 上下文 * param state 权限状态0未开启服务 1未开启权限 * return 对话框 */publicstaticDialogshowLocErrorDialog(Activity activity,int state){Dialog locErrorDialognewDialog(activity,R.style.MyDialog);View contentViewView.inflate(activity,R.layout.dialog_tip_error_loc,null);locErrorDialog.setContentView(contentView);locErrorDialog.setCanceledOnTouchOutside(true);locErrorDialog.show();TextView checkNetCancelcontentView.findViewById(R.id.tv_submit_no);TextView checkNetcontentView.findViewById(R.id.tv_submit_yes);checkNetCancel.setOnClickListener(view-{locErrorDialog.dismiss();});checkNet.setOnClickListener(view-{locErrorDialog.dismiss();Intent intentnewIntent();if(state0){//定位服务页面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();}}});returnlocErrorDialog;}Dialog样式代码语言javascriptstyle nameMyDialogparentandroid:style/Theme.Dialogitem nameandroid:windowFramenull/itemitem nameandroid:windowBackgroundandroid:color/transparent/itemitem nameandroid:windowNoTitletrue/itemitem nameandroid:backgroundcolor/transparent/item/style以上这篇Android判断定位功能是否可用的方法就是小编分享给大家的全部内容了希望能给大家一个参考。更多Android进阶指南 可以扫码 解锁《Android十大板块文档》1.Android车载应用开发系统学习指南附项目实战2.Android Framework学习指南助力成为系统级开发高手3.2024最新Android中高级面试题汇总解析告别零offer4.企业级Android音视频开发学习路线项目实战附源码5.Android Jetpack从入门到精通构建高质量UI界面6.Flutter技术解析与实战跨平台首要之选7.Kotlin从入门到实战全方面提升架构基础8.高级Android插件化与组件化含实战教程和源码9.Android 性能优化实战360°全方面性能调优10.Android零基础入门到精通高手进阶之路敲代码不易关注一下吧。ღ( ´ᴗ )