Android 实例展现如何不同程序间的Serivce调用
2010-09-15 00:10:00 来源:WEB开发网private Button b_get;
private TextView tv;
private IDemoServiceBinder dsb;
private ServiceConnection sc = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d("ReadActivity", "onServiceConnected");
dsb = IDemoServiceBinder.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
dsb = null;
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent s = new Intent("com.shinestudio.demo.demoservice");
this.bindService(s, sc, Context.BIND_AUTO_CREATE);
tv = (TextView) findViewById(R.id.TextView01);
b_get = (Button) findViewById(R.id.Button01);
b_get.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (dsb != null) {
try {
tv.setText(dsb.getPushString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
});
}
@Override
protected void onPause() {
super.onPause();
this.unbindService(sc);
}
}
赞助商链接