Android 如何将数据服务传递给活动?

Android 如何将数据服务传递给活动?,android,broadcastreceiver,android-service,intentservice,Android,Broadcastreceiver,Android Service,Intentservice,我有三节课 1.活动性 2.LocationUpdateService 3.多重标记器 1.菜单活动 @Override protected void onStart() { super.onStart(); bindService(new Intent(this, LocationUpdatesService.class), mServiceConnection, Context.BIND_AUTO_CREATE);

我有三节课

1.活动性

2.LocationUpdateService

3.多重标记器

1.菜单活动

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

        bindService(new Intent(this, LocationUpdatesService.class), mServiceConnection,
                Context.BIND_AUTO_CREATE);
    }
2.位置更新服务:(这是服务类)

3.多重营销者(活动)

我的问题是:当我打开我的MenuActivity我的Toast消息打印向广播接收器发送数据时,然后单击按钮我调用MultipleMarker.class,我无法从服务中获取值…但当我按下back按钮时,我重定向到MenuActivity,此时我获取值


我想在MultipleMarker.class活动中从我的LocationUpdateservice类获取数据…

您可以创建一个接口,并按照以下步骤获取服务的引用:

在LocationUpdateService中添加以下属性:

public class LocationUpdateService extends Service{

    // The Binder is an intern class
    public class LocalBinder extends Binder {
        //The Binder as a method which return the service
        public LocationUpdateService getService() {
            return LocationUpdateService.this;
        }
    }

    private final IBinder mBinder = new LocalBinder();

}   
然后在你的活动中:

public class MenuActivity extends AppCompatActivity {
    private LocationUpdateService mService;

    // Connection interface to the service
    private ServiceConnection mConnection = new ServiceConnection() {

        // Called when the activity connects to the service
        public void onServiceConnected(ComponentName className, IBinder service) {

        // Here you get the Service
            mService = ((LocationUpdateService.LocalBinder)service).getService();

        }

        // Called when service is disconnected
        public void onServiceDisconnected(ComponentName className) {
            mService = null;
        }
    };
}
然后您的
bindService()
方法将调用,您将获得服务的实例


注意:是异步调用的,因此您不能在调用
bindService()后立即使用服务的方法
因为您将有一个NullPointerException。

您能给我解释下一票的原因吗?您好,您可以创建接口并将其设置为活动类的侦听器,然后从服务类发送回调。@PankajKantPatel谢谢您,先生。您能给我一些参考链接吗?如何设置和获取请查看绑定服务代码,然后使用binder对象以获取服务对象的实例。我没有代码示例。您在哪里放置LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,new IntentFilter(“gpsLocationUpdate”)?D谢谢您的建议,先生..我会试试..我已经在活动的onDestroy()方法中启动了绑定服务,别忘了调用
unbindService(mConnection)
public class LocationUpdateService extends Service{

    // The Binder is an intern class
    public class LocalBinder extends Binder {
        //The Binder as a method which return the service
        public LocationUpdateService getService() {
            return LocationUpdateService.this;
        }
    }

    private final IBinder mBinder = new LocalBinder();

}   
public class MenuActivity extends AppCompatActivity {
    private LocationUpdateService mService;

    // Connection interface to the service
    private ServiceConnection mConnection = new ServiceConnection() {

        // Called when the activity connects to the service
        public void onServiceConnected(ComponentName className, IBinder service) {

        // Here you get the Service
            mService = ((LocationUpdateService.LocalBinder)service).getService();

        }

        // Called when service is disconnected
        public void onServiceDisconnected(ComponentName className) {
            mService = null;
        }
    };
}