接收器服务android上的位置null

时间:2016-06-08 10:51:01

标签: java android location locationmanager android-c2dm

当我尝试从接收器服务获取它时,我得到一个空位置。当我收到c2dm推送通知时,会调用此接收器服务。

我想要做的是查看手机的当前位置是否在一个区域内,如果是,我会显示通知,如果不是,我不会。

有什么想法吗?谢谢!

以下是代码:

        public class GCMPushReceiverService extends GcmListenerService {

    Context context = this;

    @Override
    public void onMessageReceived(String from, Bundle data) {

        if (data.containsKey("coordlat") && data.containsKey("coordlng") && data.containsKey("radius")) {
          Log.v("Location", "Location based notification");
            GPSTracker gps = new GPSTracker(context);

            if (gps.canGetLocation()) {
              Log.v("Location", "Can get location, see if its inside radius");
                Location currentLocation = gps.getLocation();

                Location location = new Location("");
                location.setLatitude(data.getDouble("coordlat"));
                location.setLongitude(data.getDouble("coordlng"));

                float distance = location.distanceTo(currentLocation);

                if (distance <= data.getFloat("radius")) {
                  Log.v("Location", "Inside radius, show notification.");
                    sendNotification();
                } else {
                  Log.v("Location", "Outside radius, dont show notification.");
                }
            } else {
                Log.v("Location", "Cant get location, show notification.");
                sendNotification(message);
            }
        }



    }

    private void sendNotification() {
        //Build notification
        NotificationCompat.Builder notiBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Title")
                .setContentText("Message")
                .setAutoCancel(true);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notiBuilder.build());
    }
}

这是获取位置:

public Location getLocation() {
        try {
            locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);

            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if(!isGPSEnabled && !isNetworkEnabled) {

            } else {
                this.canGetLocation = true;

                if (isNetworkEnabled) {

                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                        if (location != null) {

                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }

                }

                if(isGPSEnabled) {
                    if(location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                        if(locationManager != null) {
                            location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                            if(location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }
            }

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

        return location;
    }

0 个答案:

没有答案