服务未调用 BroadcastReceiver

发布于 2025-01-06 01:21:48 字数 6142 浏览 3 评论 0原文

在我的应用程序中,我需要获取用户位置,这就是我到目前为止所做的:

我有一个 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);
    }
};

}

而我的 LocationService:

public class LocationService extends Service implements LocationListener {

private LocationManager mLocationManager;
private Location mLocation;

/**
 * Class for clients to access.  Because we know this service always
 * runs in the same process as its clients, we don't need to deal with
 * IPC.
 */
public class LocationBinder extends Binder {
    public LocationService getService() {
        return LocationService.this;
    }
}

@Override
public void onCreate() {
    this.mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    this.mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {        
    return START_STICKY;
}

@Override
public void onDestroy() {
    // Tell the user we stopped.
    Toast.makeText(this, R.string.local_service_stopped, Toast.LENGTH_SHORT).show();
}

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

// This is the object that receives interactions from clients.  See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocationBinder();

public Location getLocation() {
    return this.mLocation;
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    this.mLocation = location;  
    Intent intent = new Intent(this, LocationActivity.class);
    sendBroadcast(intent);
}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

}

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

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

有人可以帮忙吗?

预先非常感谢

In my application i need to obtain user location and this is what i have done so far:

I have a LocationActivity that bounds to a service implementing LocationListener interface:

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

}

And my LocationService:

public class LocationService extends Service implements LocationListener {

private LocationManager mLocationManager;
private Location mLocation;

/**
 * Class for clients to access.  Because we know this service always
 * runs in the same process as its clients, we don't need to deal with
 * IPC.
 */
public class LocationBinder extends Binder {
    public LocationService getService() {
        return LocationService.this;
    }
}

@Override
public void onCreate() {
    this.mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    this.mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {        
    return START_STICKY;
}

@Override
public void onDestroy() {
    // Tell the user we stopped.
    Toast.makeText(this, R.string.local_service_stopped, Toast.LENGTH_SHORT).show();
}

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

// This is the object that receives interactions from clients.  See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocationBinder();

public Location getLocation() {
    return this.mLocation;
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    this.mLocation = location;  
    Intent intent = new Intent(this, LocationActivity.class);
    sendBroadcast(intent);
}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

}

What i want to achieve is to tell the activity that the location has changed, so it can call the service for the location data. I call sendBroadcast() in my service and have a BroadcastReceiver in my activity.

The service is started and stopped correctly when needed. Also the onLocationChanged method in the service is called correctly, the problem is that i am not able to get the onReceive method called. I dont really know what i am doing wrong.

Can someone help with this?

Thanks a lot in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从来不烧饼 2025-01-13 01:21:48

在sendBroadcast中,向intent添加一个action:

intent.setAction("example.package.location");

并在Activity中添加一个intentFilter:

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

In sendBroadcast, add a action to the intent:

intent.setAction("example.package.location");

and in Activity add a intentFilter as well:

filter.addAction("example.package.location");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文