Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 广播接收器未被服务调用_Android_Service_Broadcastreceiver - Fatal编程技术网

Android 广播接收器未被服务调用

Android 广播接收器未被服务调用,android,service,broadcastreceiver,Android,Service,Broadcastreceiver,在我的应用程序中,我需要获取用户位置,这是我迄今为止所做的: 我有一个LocationActivity,它绑定到实现LocationListener接口的服务: public class LocationActivity extends MapActivity { private MapView mMapView; private MapController mMapController; private List<Overlay> mMapOverlays; privat

在我的应用程序中,我需要获取用户位置,这是我迄今为止所做的:

我有一个LocationActivity,它绑定到实现LocationListener接口的服务:

public class LocationActivity extends MapActivity {

private MapView mMapView;
private MapController mMapController;   

private List<Overlay> mMapOverlays;
private Drawable drawable;
private CustomItemizedOverlay mItemizedOverlay;

private boolean mIsBound;
private LocationService mBoundService;

public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.map);

    this.mMapView = (MapView)findViewById(R.id.mapview);        
    this.mMapView.setBuiltInZoomControls(true);
    this.mMapController = this.mMapView.getController();
    this.mMapController.setZoom(18);
    this.mMapView.setClickable(true);
    this.mMapView.setEnabled(true);       

    this.mMapOverlays = this.mMapView.getOverlays();
    this.drawable = this.getResources().getDrawable(R.drawable.map_pin);
    this.mItemizedOverlay = new CustomItemizedOverlay(this.drawable);
}

@Override
protected void onResume() {
    super.onResume();      
    doBindService();
    this.registerReceiver(this.mBroadcastReceiver, new IntentFilter());
}

@Override
protected void onStart() {
    super.onStart();        
}


@Override
protected void onStop() {
    super.onStop();     
}

@Override
protected void onDestroy() {
    super.onDestroy();      
    doUnbindService();
    this.unregisterReceiver(this.mBroadcastReceiver);
}

@Override
protected boolean isRouteDisplayed() {
    return false;
}

private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        // This is called when the connection with the service has been
        // established, giving us the service object we can use to
        // interact with the service.  Because we have bound to a explicit
        // service that we know is running in our own process, we can
        // cast its IBinder to a concrete class and directly access it.
        mBoundService = ((LocationService.LocationBinder)service).getService();

        // Tell the user about this for our demo.
        Toast.makeText(LocationActivity.this, R.string.local_service_connected,
                Toast.LENGTH_SHORT).show();
    }

    public void onServiceDisconnected(ComponentName className) {
        // This is called when the connection with the service has been
        // unexpectedly disconnected -- that is, its process crashed.
        // Because it is running in our same process, we should never
        // see this happen.
        mBoundService = null;
        Toast.makeText(LocationActivity.this, R.string.local_service_disconnected,
                Toast.LENGTH_SHORT).show();
    }
};

void doBindService() {
    // Establish a connection with the service.  We use an explicit
    // class name because we want a specific service implementation that
    // we know will be running in our own process (and thus won't be
    // supporting component replacement by other applications).
    bindService(new Intent(LocationActivity.this, 
            LocationService.class), mConnection, Context.BIND_AUTO_CREATE);
    mIsBound = true;
}

void doUnbindService() {
    if (mIsBound) {
        // Detach our existing connection.
        unbindService(mConnection);
        mIsBound = false;
    }
}    

private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(LocationActivity.this, "ooooooooo", Toast.LENGTH_SHORT).show();
        Location location = LocationActivity.this.mBoundService.getLocation();

        int lat = (int) (location.getLatitude() * 1E6);
        int lng = (int) (location.getLongitude() * 1E6);
        GeoPoint point = new GeoPoint(lat, lng);
        mMapController.animateTo(point);

        OverlayItem overlayitem = new OverlayItem(point, "", "");

        mItemizedOverlay.addOverlay(overlayitem);
        mMapOverlays.add(mItemizedOverlay);
    }
};

}
我想要实现的是告诉活动位置已更改,以便它可以调用服务获取位置数据。我在服务中调用sendBroadcast(),并且在活动中有一个BroadcastReceiver

服务在需要时正确启动和停止。服务中的onLocationChanged方法也被正确调用,问题是我无法调用onReceive方法。我真的不知道我做错了什么

有人能帮忙吗


提前非常感谢

在sendBroadcast中,为意图添加操作:

intent.setAction("example.package.location");
并在活动中添加一个intentFilter:

filter.addAction("example.package.location");

感谢JohnCookie,我在intent和filter中添加了以下操作,但什么也没发生:org.goallabs.android.app.thedayafter.location.location\u READY。还有别的想法吗?非常感谢用于发送广播的意图不需要设置类,只是一个空的意图。意图=新意图();意图。设置动作(…);发送广播(意图);:)很高兴听到这个消息。祝你好运~
filter.addAction("example.package.location");