Android 在WakefulBroadcastReceiver中使用LocationListener

Android 在WakefulBroadcastReceiver中使用LocationListener,android,geolocation,location,Android,Geolocation,Location,我想在WakefulBroadcastReceiver中获取当前位置。我试过的是 public class AlarmReceiver extends WakefulBroadcastReceiver implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { private static final String TAG =

我想在
WakefulBroadcastReceiver
中获取当前位置。我试过的是

public class AlarmReceiver extends WakefulBroadcastReceiver implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
    private static final String TAG = "AlarmReceiver";

    private GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;
    private Location location;

    public void generateAlarm(Context context, long timeInMillis) {

        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(context)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();
            mGoogleApiClient.connect();
        }

        if (mLocationRequest == null) {
            mLocationRequest = new LocationRequest();
            mLocationRequest.setInterval(60000);
            mLocationRequest.setFastestInterval(5000);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        }

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmReceiver.class);
        PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        alarmManager.set(AlarmManager.RTC_WAKEUP,
                timeInMillis, alarmIntent);

        ComponentName receiver = new ComponentName(context, BootReceiver.class);
        PackageManager pm = context.getPackageManager();

        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v(TAG, "onReceive");
        generateNotification(context);
    }

    public void generateNotification(Context context) {
            Log.v(TAG, "generateNotification");//This gets printed.    
            if (location != null) {
                Log.v(TAG, "null != location");//But this doesn't printed, because location object is null.
            }
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }

    @Override
    public void onLocationChanged(Location location) {
        Log.v(TAG, "onLocationChanged : " + location.getLongitude());// this one works.
        this.location = location;// Here I am assigned the value for location object.
    }
因此,我在
onLocationChanged
中分配了location对象,但在
generateNotification()
中它仍然为空。我不知道我错在哪里