获取手机Gps地理位置并获取Gps信号强弱

在项目终需要开启gps用来获取地理位置,获取不到地理位置时发送一条广播,进行通知

1,启动服务,在服务中每隔3秒检测gps是否开启以及获取到当前的地理位置

2,将获取到的地理位置发送一条全局广播,便于接收

public class LocationService extends Service implements LocationListener {

    private String cityName; //当前城市名称
    private LocationManager locationManager;
    private Context context;


    //接收并且处理消息
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 1) {
//                Log.e("---", "获取到地理位置" + cityName);
                Intent intent = new Intent("com.city");
                intent.putExtra("address", cityName);
                context.sendBroadcast(intent);
            }
            return false;
        }
    });


    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            // 要做的事情,这里再次调用此Runnable对象,以实现每两秒实现一次的定时器操作
            checkGPSIsOpen();

        }
    };


    @Override
    public void onCreate() {
        context = this;
        super.onCreate();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        checkGPSIsOpen();
        handler.postDelayed(runnable, 3000);
        return START_STICKY;
    }

//    private void init() {
//
//
//    }


    private void checkGPSIsOpen() {
        //获取当前的LocationManager对象
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        boolean isOpen = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        if (!isOpen) {
            Intent intent = new Intent("com.city");
            intent.putExtra("address", "");
            context.sendBroadcast(intent);
        }
        List list = locationManager.getAllProviders();
        boolean bfind = false;
        for (String c : list) {
//            Log.e("---c", c);
            if (c.equals(LocationManager.NETWORK_PROVIDER)) {
                bfind = true;
                break;
            }
        }

        try {
            if (bfind) {
                Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                updateWithNewLocation(location);
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 10, this);

            }
        } catch (SecurityException e) {
            e.printStackTrace();
        }

        //开始定位
//        startLocation();
    }


    //获取对应位置的经纬度并且定位城市
    private void updateWithNewLocation(Location location) {
        double lat = 0.0, lng = 0.0;
        if (location != null) {
            lat = location.getLatitude();
            lng = location.getLongitude();
            Log.i("TAG", "经度是" + lat + "纬度是:" + lng);
        } else {
            cityName = "";
        }
        //通过经纬度获取地址,由于地址会有多个,这个和经纬度精确度有关,本实例中定义了最大的返回数2,即在集合对象中有两个值
        List
list = null; Geocoder ge = new Geocoder(context); try { list = ge.getFromLocation(lat, lng, 2); } catch (IOException e) { e.printStackTrace(); } if (list != null && list.size() > 0) { for (int i = 0; i < list.size(); i++) { Address ad = list.get(i); cityName = ad.getAdminArea() + ad.getSubAdminArea() + ad.getLocality() + ad.getFeatureName(); } } handler.postDelayed(runnable, 3000); //发送一条空消息 handler.sendEmptyMessage(1); } //位置信息更改执行方法 @Override public void onLocationChanged(Location location) { //更新当前的位置信息 updateWithNewLocation(location); } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onDestroy() { super.onDestroy(); //保存城市 //停止定位 stopLocation(); } //停止定位 private void stopLocation() { locationManager.removeUpdates(this); } }

这是在服务里检测位置的代码

接下来就是在广播中接收的代码了

public class MyBroadcast extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals("com.city")) {
            String address = intent.getStringExtra("address");
            if (TextUtils.isEmpty(address)) {
                Toast.makeText(context, "GPS未开启", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, address, Toast.LENGTH_SHORT).show();
            }

        }

    }
}

在这里需要注意的是:
1,在AndroidMainfest.xml中开启地理位置权限

2,在AndroidMainfest.xml中注册广播和服务

 




    
        
    

你可能感兴趣的:(获取手机Gps地理位置并获取Gps信号强弱)