?
首先要明白需要的情景,然后对三种方式进行选择:
(一)可以接收Service的信息(获取Service中的方法),但不可以给Service发送信息
package com.example.boundservice;
import java.util.Random;
import android.R.integer;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class LocalService extends Service {
private IBinder myBinder = new MyBinder();
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return myBinder;
}
public class MyBinder extends Binder {
LocalService getService() {
return LocalService.this;
}
}
public int getRandomNumber() {
return new Random().nextInt(100);
}
}
package com.example.boundservice;
import com.example.boundservice.LocalService.MyBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.TextView;
public class BindingActivity extends Activity implements ServiceConnection{
private LocalService localService;
private MyBinder myBinder;
boolean mBound = false;
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Intent intent = new Intent(this,LocalService.class);
bindService(intent, this, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
// Unbind from the service
if (mBound) {
unbindService(this);
mBound = false;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_binding_activity);
findViewById(R.id.button_show_binding_text).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
((TextView)findViewById(R.id.textview)).setText(+localService.getRandomNumber());
}
});
}
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
myBinder = (MyBinder)arg1;
localService = myBinder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
}
(二) 使用Messenger既可以接受Service消息,也可以发送Service消息。但是无法调用Service中的方法。因为利用Message,所以不用担心并发
?
-
Extending the Binder class
-
If your service is private to your own application and runs in the same process as the client (which is common), you should create your interface by extending the
Binder class and returning an instance of it from
onBind(). The client receives the
Binder and can use it to directly access public methods available in either the
Binder implementation(得到Service对象,从而获取Service中的方法) or even the
Service.
-
This is the preferred technique when your service is merely a background worker for your own application. The only reason you would not create your interface this way is because your service is used by other applications or across separate processes.
-
Service代码如下:
-
Activity代码如下:
-
?
-
Using a Messenger
-
If you need your interface to work across different processes, you can create an interface for the service with a
Messenger. In this manner, the service defines a
Hand