生活随笔
收集整理的這篇文章主要介紹了
【Android小知识点】判断手机是否开启了模拟位置
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
很多時(shí)候,我們要獲得用戶的真實(shí)位置,但有些用戶會(huì)使用模擬位置來模擬,這時(shí)候我們就可以要求關(guān)閉模擬位置才能正常使用
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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);if (Settings.Secure.getInt(getContentResolver(),Settings.Secure.ALLOW_MOCK_LOCATION, 0) != 0) {//模擬位置已經(jīng)打開了new AlertDialog.Builder(this).setTitle("提示").setMessage("您的模擬位置已打開,無法打卡,請(qǐng)先關(guān)閉,點(diǎn)擊確定進(jìn)行設(shè)置!").setNegativeButton("取消",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which) {// 不關(guān)閉}}).setPositiveButton("確定",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which) {// 跳轉(zhuǎn)到關(guān)閉界面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) {// 找不到開發(fā)設(shè)置界面就跳系統(tǒng)設(shè)置Intent intent = new Intent(Settings.ACTION_SETTINGS);startActivity(intent);}}}).show();} else {//模擬位置沒有打開}}
}
總結(jié)
以上是生活随笔為你收集整理的【Android小知识点】判断手机是否开启了模拟位置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。