欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android获取系统定位

程序员文章站 2022-03-27 21:45:21
1直接上代码public class LocationUtils { LocationManager locationManager; private Activity activity; public LocationUtils(Activity activity) { this.activity = activity; } /** * 判断手机GPS是否开启 * @return */ public b...

1直接上代码

public class LocationUtils {
    LocationManager locationManager;
    private Activity activity;

    public LocationUtils(Activity activity) {
        this.activity = activity;
    }

    /**
     * 判断手机GPS是否开启
     * @return
     */
    public boolean isOpen() {
        locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
        //通过GPS卫星定位,定位级别到街
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        //通过WLAN或者移动网络确定位置
        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (gps || network) {
            return true;
        }
        return false;
    }

    /**
     * 开启手机GPS
     */
    public void openGPS() {
        Intent GPSIntent = new Intent();
        GPSIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvide");
        GPSIntent.addCategory("android.intent.category.ALTERNATIVE");
        GPSIntent.setData(Uri.parse("custom:3"));

        try {
            //使用PendingIntent发送广播告诉手机去开启GPS功能
            PendingIntent.getBroadcast(activity, 0, GPSIntent, 0).send();
        } catch (PendingIntent.CanceledException e) {
            e.printStackTrace();
        }
    }

    /**
     * GPS功能已经打开-->根据GPS去获取经纬度
     */
    @SuppressLint("MissingPermission")
    public void getGPSConfi(LocationListener listener) {
        Location location;
        if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, listener);
            location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        } else {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, listener);
            location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        }

        if (location != null) {
            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
            ToastUtils.showLong("经纬度:" + latitude + "--" + longitude);
        } else {
            ToastUtils.showLong("未获取到经纬度数据");
        }
    }
}

2.调用

   requestPermissions(new String[]{ android.Manifest.permission.ACCESS_FINE_LOCATION,
                android.Manifest.permission.ACCESS_COARSE_LOCATION}, 10086);
        LocationUtils locationUtils = new LocationUtils(getActivity());
        if(!locationUtils.isOpen()){
            locationUtils.openGPS();
        }
        locationUtils.getGPSConfi(this);

 

 

 

本文地址:https://blog.csdn.net/qq_15059163/article/details/109637677

相关标签: java android