MyserviceDelegate delegate;
private MyServiceConnection connection;
private class MyServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
delegate = (MyserviceDelegate) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connection = new MyServiceConnection();
Intent it = new Intent();
it.setClass(this, Myservice.class);
bindService(it, connection, BIND_AUTO_CREATE);
}
//代理
public interface MyserviceDelegate {
public void helloWorld();
}
// 服務(wù)
public class Myservice extends Service {
private class MyBinder extends Binder implements MyserviceDelegate{
@Override
public void helloWorld() {
sayHelloWorld();
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new MyBinder();
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
public void sayHelloWorld() {
}
}