【Android小知识点】判断手机是否开启了模拟位置

很多时候,我们要获得用户的真实位置,但有些用户会使用模拟位置来模拟,这时候我们就可以要求关闭模拟位置才能正常使用


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 {
			//模拟位置没有打开
		}

	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值