Android Gps/位置信息開關(guān)檢測(cè)

6.0以上獲取位置信息,需要?jiǎng)討B(tài)申請(qǐng),這里討論當(dāng)位置權(quán)限申請(qǐng)同意了,但是位置信息關(guān)閉了(其實(shí)就是6.0以前的gps開關(guān))高精度定位也會(huì)獲取失敗?。ňW(wǎng)上有的檢測(cè)方法在某些手機(jī)上會(huì)檢測(cè)失敗,下列的方式親測(cè)有效)


位置信息開關(guān)
package com.guoshikeji.xiaoxiangDriver.utils;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;
    
/**
 * Created by tyl
 * 2019/5/22/022
 * Describe:
 */
public class GpsUtil {
    /**
     * 判斷GPS是否開啟,GPS或者AGPS開啟一個(gè)就認(rèn)為是開啟的
     * @param context
     * @return true 表示開啟
     */
    public static final boolean isOPen(final Context context) {
        if (context==null){
            return true;
        }
        int locationMode = 0;
        String locationProviders;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            try {
                locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
            } catch (Settings.SettingNotFoundException e) {
                e.printStackTrace();
                return false;
            }
            return locationMode != Settings.Secure.LOCATION_MODE_OFF;
        } else {
            locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            return !TextUtils.isEmpty(locationProviders);
        }
    }

    /**
     * 跳轉(zhuǎn)設(shè)置 打開GPS
     * @param context
     */
    public static final void openGPS(Context context) {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        context.startActivity(intent);
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容