将广播从服务传递到活动

时间:2015-08-27 13:37:23

标签: android

我有TrackingService组件,可根据众包来跟踪我所在城市的公交车位置。在TrackingService类中,我有变量pLong, pLat,以便在onLocatiochChanged()中计算出它们时存储纬度和经度。 TrackingService在后台运行,然后将数据传输到服务器。我有一个Map Activity来显示总线的位置,在MainActivity中选择的用户(作为Filter)。

应用程序启动时,后台TrackingService在MainActivity中启动。

我在调用pLat, pLong时尝试将onLocationChanged()Map传递到onLocationChanged()活动,以显示用户在公交车位置旁边的当前位置BroadcastReceiver的帮助。

我已经调试了我的代码并面临问题,我在0.0活动的BroadcastReiceiver中获取值lat, lng map,同时调用了bReceiver

如何让它在BroadcastReceiver bReceiver中检索lat和lng vlaues?

TrackingService类:

public class TrackingService extends Service implements
        LocationListener {
    public double pLong;
    public double pLat;
    ...
        @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        detectLocation();
        return START_STICKY;
    }
    private void detectLocation() {
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30 * 1000, 0,
                this);
    }
    @Override
    public void onLocationChanged(Location location) {

        if (location != null) {
        pLong = location.getLongitude();
        pLat = location.getLatitude();

        Intent intent = new Intent(Map.RECEIVE_latLng);
        Bundle extras = new Bundle();
        extras.putDouble("lng", pLong);
        extras.putDouble("lat", pLat);
        intent.putExtras(extras);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
        System.out.println("ABC TrackingService: "+ pLat + " ; " + pLong);
           .....

     }  

}

地图活动:

    public class Map extends FragmentActivity implements OnMapReadyCallback   {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);

        LocalBroadcastManager bManager = LocalBroadcastManager.getInstance(this);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(RECEIVE_latLng);
        bManager.registerReceiver(bReceiver, intentFilter);

    }

public static final String RECEIVE_latLng = "com.bustracker.RECEIVE_latLng";
    private BroadcastReceiver bReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(RECEIVE_latLng)) {
             Bundle extras = getIntent().getExtras();
             if(!extras.isEmpty() && extras != null){
                 double lng = extras.getDouble("lng");
                 double lat = extras.getDouble("lat");
                 LatLng ll = new LatLng(lng,lat);
                 MarkerOptions markerOpt = new MarkerOptions().title("My Location")
                            .position(ll);
                 System.out.println("ABC map: "+ lat + " ; " + lng);
                 myLocatMarker = map.addMarker(markerOpt);

             }
            }
        }
    };



      }

输出:

08-27 15:25:48.116: I/System.out(3260): ABC TrackingService: 63.86896885 ; 13.66557022
08-27 15:25:48.146: I/System.out(3260): ABC map: 0.0 ; 0.0

0 个答案:

没有答案