繁体   English   中英

从服务接收活动中的广播

[英]Receive broadcast in the activity from the service

我有TrackingService组件,可以基于众包跟踪城市中公交车的位置。 在TrackingService类中,我具有变量pLong, pLat和pLat,用于在onLocatiochChanged()中计算纬度和经度时将其存储。 TrackingService在后台运行,然后将数据传输到服务器。 我有一个地图活动来显示公交车的位置,该用户在MainActivity(作为筛选器)中选择。

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

我想传递的pLat, pLongonLocationChanged()Map活动时onLocationChanged()被调用,以显示与广播接收器的帮助下,公交车的位置旁边的用户的当前位置。

我已经调试了代码,并遇到了问题,即在map活动中的BroadcastReiceiver中lat, lnglat, lng的值bReceiver 0.0 ,并且bReceiver也被调用。

我如何使其能够在BroadcastReceiver bReceiver检索bReceiver

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

内部onLocationChanged

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

onReceive

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

暂无
暂无

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

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