Android 进程间服务通信
2010-04-12 03:39:00 来源:WEB开发网public String getCurrentDataTime() {
return (getCurrentDataTimeImpl());
}
};
synchronized private String getCurrentDataTimeImpl() {
Date date=new Date();
return (date.toLocaleString());
}
@Override
public IBinder onBind(Intent intent) {
return (binder);
}
}
3.现在编写一个Activity,来消费这个服务:
Java代码
public class ServicesDemo2 extends Activity {
private IDataTimeService service = null;
private Intent serviceIntent = null;
private WebView browser;
private ServiceConnection svcConn = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder binder) {
service = IDataTimeService.Stub.asInterface(binder);
browser.postDelayed(new Runnable() {
public void run() {
updateForecast();
}
}, 1000);
}
public void onServiceDisconnected(ComponentName className) {
service = null;
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
browser = (WebView) findViewById(R.id.webkit);
serviceIntent = new Intent(this, DataTimeServiceImpl.class);
//绑定一个服务
bindService(serviceIntent, svcConn, BIND_AUTO_CREATE);
}
@Override
public void onResume() {
super.onResume();
/**
注册广播消息的接收者,在android中服务与Activity的通信依靠广播发送消息,然后由Activity注册的接收者
接收到消息,进行相应的处理这里使用DataTimeServiceImpl.BROADCAST_ACTION来过滤,表明它只接收
更多精彩
赞助商链接