美文网首页面试题
获取手机信息,位置信息

获取手机信息,位置信息

作者: 名字不想带英文 | 来源:发表于2019-10-31 14:23 被阅读0次

    获取手机信息的Helper

    import android.Manifest;
    import android.app.Activity;
    import android.content.Context;
    import android.content.pm.PackageManager;
    import android.os.Build;
    import android.provider.Settings;
    import android.telephony.TelephonyManager;
    import android.text.TextUtils;
    
    import androidx.core.app.ActivityCompat;
    
    import com.ta.utdid2.android.utils.NetworkUtils;
    
    public class PhoneHelper {
        //需要注意的是TelephonyManager除了在xml里面要加权限,同时还需要动态申请READ_PHONE_STATE权限,
        private static TelephonyManager telephonyManager;
        private Activity activity;
    
        public PhoneHelper(Activity activity) {
            this.activity = activity;
    //        String str = Looper.myLooper() == Looper.getMainLooper() ? "主线程" : "非主线程";
        }
    
        public class PhoneBean {//自定义的对象,需要更多参数的话自己添加
            private String netWorkType = "没有网络";
            private String SystemType = "android";
            private String DeviceModel;//设备型号:nikel
            private String SystemVerson;//版本信息:6.0
            private String deviceName;//品牌:Xiaomi
    
            private String deviceId = "0000003982923892827";//设备标志:IMEI:2378283982923892827
            private String Operators;//运营商:中国移动
    
            public String getSystemType() {
                return SystemType;
            }
    
            public void setSystemType(String systemType) {
                SystemType = systemType;
            }
    
    
            public String getDeviceId() {
                return deviceId;
            }
    
            public void setDeviceId(String deviceId) {
                this.deviceId = deviceId;
            }
    
            public String getDeviceModel() {
                return DeviceModel;
            }
    
            public void setDeviceModel(String deviceModel) {
                DeviceModel = deviceModel;
            }
    
            public String getOperators() {
                return Operators;
            }
    
            public void setOperators(String operators) {
                Operators = operators;
            }
    
            public String getSystemVerson() {
                return SystemVerson;
            }
    
            public void setSystemVerson(String systemVerson) {
                SystemVerson = systemVerson;
            }
    
            public String getDeviceName() {
                return deviceName;
            }
    
            public void setDeviceName(String deviceName) {
                this.deviceName = deviceName;
            }
    
            public String getNetWorkType() {
                return netWorkType;
            }
    
            public void setNetWorkType(String netWorkType) {
                this.netWorkType = netWorkType;
            }
        }
    
        public PhoneBean getPhoneMsg() {
            PhoneBean phoneBean = new PhoneBean();
            phoneBean.setNetWorkType(NetworkUtils.isWifi(activity) ? "wifi" : "4g");
            phoneBean.setDeviceModel(Build.DEVICE);
            phoneBean.setDeviceName(Build.MODEL);
            phoneBean.setSystemVerson(Build.VERSION.RELEASE);
            phoneBean.setDeviceId(getAndroidId(activity));
    
    //        因为谷歌对于用户权限越来越严格,直接不要telephonyManager了,用androidId来判断
    //        if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
    //            telephonyManager = (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE);
    //            String Operators = "中国移动";
    //            String DeviceId = "0000003982923892827";
    //            if (telephonyManager != null) {
    //                String IMSI = telephonyManager.getSubscriberId();
    //                if (IMSI != null) {
    //                    if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
    //                        Operators = "中国移动";
    //                    } else if (IMSI.startsWith("46001")) {
    //                        Operators = "中国联通";
    //                    } else if (IMSI.startsWith("46003")) {
    //                        Operators = "中国电信";
    //                    }
    //                }
    //                if (telephonyManager.getDeviceId() != null) {
    //                    DeviceId = telephonyManager.getDeviceId();
    //                }
    //            }
    //            phoneBean.setDeviceId(DeviceId);
    //            phoneBean.setOperators(Operators);
    //        } else {
    //            // TODO: Consider calling 如果权限是拒绝的就随便填些数据,一定要拿到DeviceId的话请强制要求给权限
    //            phoneBean.setDeviceId("0000003982923892827");
    //            phoneBean.setOperators("中国移动");
    //        }
            return phoneBean;
        }
    
        /**
         * 获取AndroidId
         */
        private String getAndroidId(Context context) {
            if (context == null) {
                return "";
            }
            String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            return (TextUtils.isEmpty(androidId) ? "0000003982923892827" : androidId);
        }
    }
    

    下面是获取位置的,像上面说的,动态申请权限需要先在AndroidManifest.xml声明,分别是ACCESS_FINE_LOCATION和ACCESS_COARSE_LOCATION权限

    import android.Manifest;
    import android.app.Activity;
    import android.content.Context;
    import android.content.pm.PackageManager;
    import android.location.Address;
    import android.location.Geocoder;
    import android.location.Location;
    import android.location.LocationManager;
    import android.widget.Toast;
    
    import androidx.core.app.ActivityCompat;
    
    import com.yanzhenjie.permission.Action;
    import com.yanzhenjie.permission.AndPermission;
    
    import java.io.IOException;
    import java.util.List;
    
    import static android.content.Context.LOCATION_SERVICE;
    
    /**
     * Created by admin on 2017/11/6.
     */
    public class LocationHelper {
        private Activity activity;
        private LocationInterface locationInterface;
    
        public LocationHelper(Activity activity, LocationInterface locationInterface) {
            this.activity = activity;
            this.locationInterface = locationInterface;
            checkLocationPermission();
        }
    
        private String judgeProvider(LocationManager locationManager) {
            List<String> prodiverlist = locationManager.getProviders(true);
            if (prodiverlist.contains(LocationManager.NETWORK_PROVIDER)) {
                return LocationManager.NETWORK_PROVIDER;
            } else if (prodiverlist.contains(LocationManager.GPS_PROVIDER)) {
                return LocationManager.GPS_PROVIDER;
            } else {
    //华为如果没打开gps找不到位置提供器
                Toast.makeText(activity, "没有可用的位置提供器", Toast.LENGTH_SHORT).show();
            }
            return null;
        }
    
        private void getLocation(Context context) {//获取位置
            LocationManager locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);//获得位置服务
            try {
                if (locationManager != null) {
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                        && ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    return null;
                }
                if (locationManager != null) {
                    List<String> providers = locationManager.getProviders(true);
                    Location bestLocation = null;
                    for (String providerStr : providers) {
                        Location l = locationManager.getLastKnownLocation(providerStr);
                        if (l == null) {
                            continue;
                        }
                        if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
                            // Found best last known location: %s", l);
                            bestLocation = l;
                        }
                    }
                    return bestLocation;
                }
    
                    //String provider = judgeProvider(locationManager);//位置提供器
                   // if (provider != null) {//有位置提供器的情况
                       // //为了压制getLastKnownLocation方法的警告
                      //  if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                              //  && ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                          //  return;
                      //  }
    
                       // //Location location = locationManager.getLastKnownLocation(provider);//这个可能会搜不到
                       // if (location != null) {
                          //  Geocoder geocoder = new Geocoder(activity);
                          //  try {
                            //    List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
                             //   Address address = addresses.get(0);
                            //    if (locationInterface != null) {
                               //     locationInterface.location(address);
                            //    }
                         //   } catch (IOException e) {
                            //   e.printStackTrace();
                          //  }
                      //  }
                    //}
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private void checkLocationPermission() {
    //        AndPermission是别人的框架,百度一下就有
    //        虽然位置权限需要动态申请,但他并不是特别重要的功能,所以每次打开页面随意申请一下就行,也不需要管什么回调
            AndPermission.with(activity)
                    .runtime()
                    .permission(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION)
                    .onGranted(new Action<List<String>>() {
                        @Override
                        public void onAction(List<String> data) {
                            //允许了就去获取位置
                            getLocation(activity);
                        }
                    })
                    .start();
        }
    
        public interface LocationInterface {
            void location(Address address);
        }
    }
    

    使用方法

    new LocationHelper(this, new LocationHelper.LocationInterface() {
                @Override
                public void location(Address address) {
                    LogUtil.i(address.toString() + "");
                }
            });
    

    相关文章

      网友评论

        本文标题:获取手机信息,位置信息

        本文链接:https://www.haomeiwen.com/subject/gzvzvctx.html