【发布时间】:2018-02-28 09:12:23
【问题描述】:
我需要检查是否有任何虚假应用在开发者设置中使用了位置信息。所以我需要让用户将其更改为开发人员设置。
但我需要使用而不使用位置。我尝试了这种方法,但即使我启用了虚假应用位置,也会显示未启用的 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(); // 没有位置怎么用?
那么还有其他解决方案吗?
【问题讨论】:
标签: android android-studio android-location