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

时间:2018-01-17 07:58:12

标签: android broadcastreceiver android-service intentservice

我有三个班级

  

1.MenuActivity

     

2.LocationUpdateService

     

3.MultipleMarker

1.MenuActivity

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

        bindService(new Intent(this, LocationUpdatesService.class), mServiceConnection,
                Context.BIND_AUTO_CREATE);
    }

2.LocationUpdateService:(这是服务类)

   private  void sendMessageToActivity(Location l, String msg) {
        Intent intent = new Intent("GPSLocationUpdates");
        // You can also include some extra data.
        intent.putExtra("Status", msg);
        Bundle b = new Bundle();
        b.putParcelable("Location", l);
        intent.putExtra("Location", b);
        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
            Toast.makeText(context,"Sending Data to BroadCast Receiver",Toast.LENGTH_LONG).show();
    }

3.MultipleMarker (活动)

 LocalBroadcastManager.getInstance(this).registerReceiver(
                mMessageReceiver, new IntentFilter("GPSLocationUpdates"));



 private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Get extra data included in the Intent
            String message = intent.getStringExtra("Status");
            Bundle b = intent.getBundleExtra("Location");

            Log.d("messagesshow","****** "+message);

           Location   lastKnownLoc = (Location) b.getParcelable("Location");
            if (lastKnownLoc != null) {

                Log.d("messagesshow","*****    "+String.valueOf(lastKnownLoc.getLatitude()));

            }

             Toast.makeText(context, "Data Receiver called", Toast.LENGTH_SHORT).show();
        }
    };

我的问题是:当我打开我的MenuActivity我的Toast消息打印发送数据到BroadCast接收器然后点击按钮我正在调用MultipleMarker.class那里我无法从服务中获取值...但是当我按下后退按钮时,我重定向到MenuActivity,那时我得到了值......

我希望从我的LocationUpdateservice类中获取数据到我的MultipleMarker.class活动中......

1 个答案:

答案 0 :(得分:2)

您可以按照以下步骤创建界面并获取服务参考:

在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();

}   

然后在你的MenuActivity中:

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()方法会调用onServiceConnected(),您将获得服务的实例。

注意onServiceConnected()是异步调用的,所以在调用bindService()之后就不能使用Service的方法,因为你会有一个NullPointerException。