【问题标题】:Listen onLocationChanged from activities收听活动中的位置更改
【发布时间】:2013-12-15 12:27:11
【问题描述】:

我有一个GPSTracker 课程extends Service implements LocationListener。 并且GPSTracker 也覆盖了onLocationChanged 方法。

在我的MainActivity 中,我创建了一个GPSTracker 的实例,并使用我在GPSTracker 类中声明的自定义方法来获取纬度/经度。

如何让我的MainActivityGPSTrackeronLocationChanged 被触发时得到通知?

GPSTracker

public class GPSTracker extends Service implements LocationListener {
    ...

    Location location; // location
    double latitude; // latitude
    double longitude; // longitude

    ...

    public GPSTracker(Context context) {
        this.mContext = context;
        getLocation();
    }

    public Location getLocation() {
        ...
        return location;
    }


    public void stopUsingGPS(){
        ...
    }


    public double getLatitude(){
        if(location != null){
            latitude = location.getLatitude();
        }
        return latitude;
    }

    public double getLongitude(){
        if(location != null){
            longitude = location.getLongitude();
        }
        return longitude;
    }

    public boolean canGetLocation() {
        return this.canGetLocation;
    }

    public void showSettingsAlert(){
        ...
        // Showing Alert Message
        alertDialog.show();
    }

    @Override
    public void onLocationChanged(Location location) {
        // do some stuff
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

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

}

【问题讨论】:

  • 实现观察者模式(接口回调)

标签: android gps android-activity location


【解决方案1】:

我看到了两种通知您的 MainActivity 的方法:

首先,您可以创建一个在 MainActivity 上实现并由 GPSTracker 触发的侦听器。

其次,可以看一下BroadcastReceiver。只需在您的主要活动中添加一个广播接收器。在您的方法 OnLocationChanged 上,您只需创建一个新意图:

Intent intent = new Intent("LocationChanged");
intent.setType("text/plain");
sendBroadcast(intent);

我认为第一个解决方案不是最简单的,而是更好的。

【讨论】:

  • 第一个选项听起来不错。你能给我一些简单的例子来说明如何做到这一点吗?谢谢
  • 我想你可以试试下面的例子:link
  • 谢谢 Alex R。你救了我 :)
【解决方案2】:

您可以从您的服务广播更改。

Intent broadcastIntent = new Intent("com.example.broadcast.gps.location_change");
broadcastIntent .putDouble("latitude", latitude);
broadcastIntent .putDouble("longitude", longitude);
sendBroadcast(broadcastIntent);

然后收听你MainActivity的广播。

BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        double latitude = intent.getDoubleExtra("latitude", 0.0);
        double longitude = intent.getDoubleExtra("longitude", 0.0);
    }
};
registerReceiver(receiver, new IntentFilter("com.example.broadcast.gps.location_change"));

【讨论】:

    【解决方案3】:

    您应该在 MainActivity 中声明一个方法,如下所示

        public void notifyLocationChanged(Double lat, Double lng){
         //do some thing
        }
    

    在 GPSTracker 类中的 onLocationChanged 方法

     @Override
    public void onLocationChanged(Location location) {
       ((MainActivity)mContext).notifyLocationChanged(location.getLatitude(),location.getLongitude())
    }
    

    【讨论】:

    • 我试过这个,但应用程序崩溃和错误被抛出。 java.lang.ClassCastException: android.app.Application cannot be cast to com.x.MainActivity at com.x.classes.GPSTracker.onLocationChanged(GPSTracker.java:191)
    • 在 MainActivity 中创建 GPSTracker 的实例,如下所示: GPSTracker gps = new GPSTracker(MainActivity.this);我觉得没问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    相关资源
    最近更新 更多