简体   繁体   English

从服务接收活动中的广播

[英]Receive broadcast in the activity from the service

I have TrackingService component to track the location of the buses in my city based on Crowdsourcing. 我有TrackingService组件,可以基于众包跟踪城市中公交车的位置。 In the TrackingService class I have variable pLong, pLat to stored the latitude and longitude when they are calaculated in the onLocatiochChanged() . 在TrackingService类中,我具有变量pLong, pLat和pLat,用于在onLocatiochChanged()中计算纬度和经度时将其存储。 The TrackingService is operating in the background, then the data is transmitted to the server. TrackingService在后台运行,然后将数据传输到服务器。 I have an Map Activity to display the location of the buses, the user selected in the MainActivity( as Filter). 我有一个地图活动来显示公交车的位置,该用户在MainActivity(作为筛选器)中选择。

The background TrackingService is started in the MainActivity when the app launches. 应用启动时,后台TrackingService在MainActivity中启动。

I am trying to pass the pLat, pLong from the onLocationChanged() to the Map activity when the onLocationChanged() is being invoked to display the user's current location beside the bus's locations with the aid of BroadcastReceiver. 我想传递的pLat, pLongonLocationChanged()Map活动时onLocationChanged()被调用,以显示与广播接收器的帮助下,公交车的位置旁边的用户的当前位置。

I have debugged my code and facing problem that I am getting 0.0 for the values lat, lng in the BroadcastReiceiver in the map activity also the bReceiver is being invoked. 我已经调试了代码,并遇到了问题,即在map活动中的BroadcastReiceiver中lat, lnglat, lng的值bReceiver 0.0 ,并且bReceiver也被调用。

How can I get it to work to retrieve the lat and lng vlaues in the BroadcastReceiver bReceiver ? 我如何使其能够在BroadcastReceiver bReceiver检索bReceiver

TrackingService class: 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);
           .....

     }  

}

Map activity: 地图活动:

    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);

             }
            }
        }
    };



      }

output: 输出:

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

Inside onLocationChanged 内部onLocationChanged

    Intent intent = new Intent(Map.RECEIVE_latLng);
    intent.putExtra("location",location);
    sendBroadcast(intent);

onReceive onReceive

Location location = intent.getParcelableExtra("location");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM