我需要检查在开发者设置中是否有使用位置的虚假应用。因此我需要让用户将其更改为开发人员设置。
但我需要在不使用位置的情况下使用。我试过这个方法但是这显示没有启用toast消息,尽管我启用了假应用程序位置。
public static boolean isMockLocationEnabled(Context context)
{
boolean isMockLocation = false;
try {
//if marshmallow
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
AppOpsManager opsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
isMockLocation = (opsManager.checkOp(AppOpsManager.OPSTR_MOCK_LOCATION, android.os.Process.myUid(), BuildConfig.APPLICATION_ID)== AppOpsManager.MODE_ALLOWED);
} else {
// in marshmallow this will always return true
isMockLocation = !android.provider.Settings.Secure.getString(context.getContentResolver(), "mock_location").equals("0");
}
} catch (Exception e) {
return isMockLocation;
}
return isMockLocation;
}
MainActivity:
@Override
protected void onStart() {
super.onStart();
if (AppUtils.isMockLocationEnabled(this)) {
Log.e("Location..............", "enabled");
} else {
Log.e("Location..............", "not enabled"); //it always goes here in 8.0
}
}
所以没有位置,如何通过?因为我只需要检查假位置是否正在使用?
// location.isFromMockProvider(); //没有位置怎么用?
那么还有其他解决方案吗?
答案 0 :(得分:1)
您可以检查Mock选项是否为ON:
if (Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.ALLOW_MOCK_LOCATION).equals("0"))
return false;
else
return true;
或者您可以检测是否有使用Mock的应用程序:
boolean isExisted = false;
PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo applicationInfo : packages) {
try {
PackageInfo packageInfo = pm.getPackageInfo(applicationInfo.packageName,
PackageManager.GET_PERMISSIONS);
String[] requestedPermissions = packageInfo.requestedPermissions;
if (requestedPermissions != null) {
for (int i = 0; i < requestedPermissions.length; i++) {
if (requestedPermissions[i]
.equals("android.permission.ACCESS_MOCK_LOCATION")
&& !applicationInfo.packageName.equals(context.getPackageName())) {
isExisted = true;
break;
}
}
}
} catch (NameNotFoundException e) {
Log.e("Got exception " , e.getMessage());
}
}