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

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

在设置中实现方案

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

在AndroidManifest.xml中注册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;
    }
    

}

你可能感兴趣的:(android)