很多时候,我们要获得用户的真实位置,但有些用户会使用模拟位置来模拟,这时候我们就可以要求关闭模拟位置才能正常使用
package com.example.testchecklocalmode;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Settings.Secure.getInt(getContentResolver(),
Settings.Secure.ALLOW_MOCK_LOCATION, 0) != 0) {
//模拟位置已经打开了
new AlertDialog.Builder(this)
.setTitle("提示")
.setMessage("您的模拟位置已打开,无法打卡,请先关闭,点击确定进行设置!")
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// 不关闭
}
})
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// 跳转到关闭界面
try {
ComponentName componentName = new ComponentName(
"com.android.settings",
"com.android.settings.DevelopmentSettings");
Intent intent = new Intent();
intent.setComponent(componentName);
intent.setAction("android.intent.action.View");
startActivity(intent);
} catch (Exception e) {
// 找不到开发设置界面就跳系统设置
Intent intent = new Intent(
Settings.ACTION_SETTINGS);
startActivity(intent);
}
}
}).show();
} else {
//模拟位置没有打开
}
}
}