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


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

    在设置中实现方案

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

    在AndroidManifest.xml中注册receiver并添加特殊的权限
            
    
            
                    
                    
                
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    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;
        }
        
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
  • 相关阅读:
    springboot 使用shiro集成阿里云短信验证码
    从知识到智慧:知识图谱还要走多远?
    Python绘图报错解决:The factorplot function has been renamed to catplot.
    导出conda某个环境配置,并再另一台机器安装
    主数据管理是数字化转型成功的基石——江淮汽车案例分享
    rt_thread生成工程模板
    WordPress CVE-2022-4230复现分析
    RabbitMQ 消息应答
    腾讯云4核8G服务器CVM S5性能测评及优惠价格表
    k-均值聚类算法 Primary
  • 原文地址:https://blog.csdn.net/qq_28534581/article/details/133019006