【问题标题】:Android how to retrieve data from a Service using an IntentAndroid 如何使用 Intent 从服务中检索数据
【发布时间】:2017-12-07 14:07:47
【问题描述】:

我有一个在单独的 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";

我想使用另一个类的意图从中检索 mLastLocation,以将该位置作为 func MyFunc 的输入传递

如果我使用

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

可以编译,但是传递的Location对象为空

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

注意:服务正常运行并按设计进行更新。 谢谢。

【问题讨论】:

  • 为什么不能使用广播接收器?

标签: android android-intent service extra


【解决方案1】:

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

【讨论】:

  • 不起作用,无法定位经纬度。我必须先在变量 lat 和 lon 上使用 putDoubleExtra 才能使用 getDoubleExtra 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-24
  • 1970-01-01
  • 2017-04-18
相关资源
最近更新 更多