簡體   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