Android Method for Determining whether Location Function is Available
- 2021-09-24 23:43:20
- OfStack
Whether the location function is available or not is determined by the location service and location authority:
Judge location service:
/**
* Does the mobile phone turn on the location service, and if not, all app You will not be able to use the positioning function
*/
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;
}
Determine positioning authority:
/**
* Check the permission list
*
* @param context
* @param op This value is defined by hide Go, go AppOpsManager Class source code search, such as location permission AppOpsManager.OP_GPS==2
* @param opString Such as judging positioning authority AppOpsManager.OPSTR_FINE_LOCATION
* @return @see If the return value AppOpsManagerCompat.MODE_IGNORED Indicates that it is disabled
*/
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;
}
Check the permissions first when calling:
/**
* Check location services, permissions
*/
private void checkLocationPermission() {
if (!AppUtil.isLocServiceEnable(this)) {// Check whether the location service is turned on
if (netErrorDialog == null || !netErrorDialog.isShowing()) {
locErrorDialog = DialogUtil.showLocErrorDialog(activity, 0);
}
} else {// Detects whether the user denies the currently applied positioning permission
int checkResult = AppUtil.checkOp(this, 2, AppOpsManager.OPSTR_FINE_LOCATION);// Among them 2 Representative AppOpsManager.OP_GPS If you want to determine the permissions of the floating box, the 2 Parameters need to be replaced by 24 I.e. AppOpsManager . OP_SYSTEM_ALERT_WINDOW And, section 3 Parameters need to be replaced by 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);
}
}
}
}
If you can't use it, a dialog box will pop up, and according to 1 or 2, you can judge the jump page:
/**
* Unable to locate dialog box
*
* @param activity Context
* @param state Permission status 0 The service is not started 1 , permissions are not opened
* @return Dialog box
*/
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) {
// Location Services page
intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
} else {
// Application Details page
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) {
// If the page cannot be opened, go to the Settings page
intent.setAction(Settings.ACTION_SETTINGS);
try {
activity.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
});
return locErrorDialog;
}
Dialog Style:
<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>