先看代码:
import com.gdp2852.demo.service.BackgroundService.MyServiceBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
public class Demo_ServiceActivity extends Activity {
public final static String Tag = "Demo_Service";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.bindBtn).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
bindService(new Intent(Demo_ServiceActivity.this,BackgroundService.class)
,srvConn,BIND_AUTO_CREATE);
}
});
findViewById(R.id.unbindBtn).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
unbindService(srvConn);
}
});
}
ServiceConnection srvConn = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(Tag, "onServiceConnected "+name);
MyServiceBinder binder = (MyServiceBinder) service;
binder.doLog(" Demo_ServiceActivity_CONTENT");
}
public void onServiceDisconnected(ComponentName name) {
//在unbind和stop service都没有调用此函数
Log.d(Tag, "onServiceDisconnected:"+name);
}
};
}
Service的代码:
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class BackgroundService extends Service {
private MyServiceBinder binder = new MyServiceBinder();
private NotificationManager notificationMrg;
public void onCreate() {
super.onCreate();
notificationMrg = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
displayNotificationMessage("starting Background Service");
doInBackground();
}
private void doInBackground() {
new Thread(new Runnable() {
public void run() {
//TODO do what you wanna to do
}
}).start();
}
public void onDestroy() {
displayNotificationMessage("stopping Background Service");
super.onDestroy();
}
private void displayNotificationMessage(String message) {
Notification notification = new Notification(R.drawable.sina, message,
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, Demo_ServiceActivity.class), 0);
notification.setLatestEventInfo(this, "Background Service", message,
contentIntent);
notificationMrg.notify(R.id.app_notification_id, notification);
}
public IBinder onBind(Intent intent) {
return binder;
}
class MyServiceBinder extends Binder{
void doLog(String logContent){
Log.d(Demo_ServiceActivity.Tag, "invoke MyServiceBinder doLog:"+logContent);
}
}
}
ServiceConnection srvConn = new ServiceConnection() {
public void onServiceConnected
在ServiceConnection连接的时会,可以从onServiceConnected()中拿到IBinder引用,这样就可以从通过IBinder调用Service的方法了
另外,需注意:
1 如果onBinder返回的是null, 则不会调用onServiceConnected()
2 unbindService()却不会调用onServiceDisconnected接口. unbindService只是告诉系统,你的APP已经跟这个service没什么关系了,
系统有权回收这个Service。