android 默认开启谷歌定位精准度

摘要:本文主要提供一种默认开启谷歌定位精准度开关的方案。通过调试时打开/关闭开关对比SettingsProvider的数据变化,在开机收到ACTION_BOOT_COMPLETED广播后,主动修改并填充数据,实现默认打开的需求。

在设置中实现方案

因为设置属于原生系统的应用,权限和级别都比较高且全,一般与系统强相关的功能或需求加载设置中可以很好地避免一些问题。

在AndroidManifest.xml中注册receiver并添加特殊的权限
        <uses-permission android:name="com.google.android.providers.settings.permission.WRITE_GSETTINGS" />

        <receiver android:name=".BootCompleteReceiver">
            <intent-filter>    
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
BootCompleteReceiver.java
package com.android.settings;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Handler;
import android.provider.Settings;

public class BootCompleteReceiver extends BroadcastReceiver {
    private static String TAG ="BootCompleteReceiver";
    private Context mContext;
    private Runnable mAcceptImproveLocationAccuracyRunnable = new Runnable() {
        public void run() {
            int mode = Settings.System.getInt(mContext.getContentResolver(), "location_mode_changed", 0);
            ContentResolver localContentResolver = mContext.getContentResolver();
            ContentValues localContentValues = new ContentValues();
            localContentValues.put("name", "network_location_opt_in");
            localContentValues.put("value", 1);
            localContentResolver.insert(Uri.parse("content://com.google.settings/partner"), localContentValues);

            if(mode == 0){
                Settings.Secure.setLocationProviderEnabled(localContentResolver, "network", true);
            }
        }
    };

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        mContext = context;
        if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
            if (isAppExist("com.google.android.gms")) {
                new Handler().postDelayed(mAcceptImproveLocationAccuracyRunnable, 10000);
            }
        }
    }

    private boolean isAppExist(String pkgName) {
        ApplicationInfo info;
        try {
            info = mContext.getPackageManager().getApplicationInfo(pkgName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            info = null;
        } 
        return info != null;
    }
    

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值