Android如何使用Intent从服务检索数据

时间:2017-12-07 14:07:48

标签: android android-intent service extra

我有一个位于单独的java上的位置服务:

public class GPSService extends Service implements LocationListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {

    Intent intent;
    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    Location mLastLocation;
    String lat, lon;
    public static String str_receiver = "servicetutorial.service.receiver";

我想使用来自另一个类的intent从中检索mLastLocation,以将该Location作为func MyFunc的输入传递

如果我使用

startService(new Intent(this, GPSService.class));
GPSService obj = new GPSService ();
MyFunc(obj.mLastLocation);

它编译,但传递的Location对象为null

我需要使用put和get Extras吗?在Location变量的情况下如何使用它?我还需要使用put吗?

注意:服务没有问题,并按设计更新。 感谢。

1 个答案:

答案 0 :(得分:0)

您可以将纬度和经度作为两个不同的double值传递,并使用getDoubleExtra检索