Android 使用 aidl 文件创建服务示例
2010-03-04 03:26:00 来源:WEB开发网/**
注册广播消息的接收者,在android中服务与Activity的通信依靠广播发送消息,然后由Activity注册的接收者
接收到消息,进行相应的处理这里使用DataTimeServiceImpl.BROADCAST_ACTION来过滤,表明它只接收
DataTimeService中的调用sendBroadcast(broadcast)发送的广播消息;
*/
registerReceiver(receiver, new IntentFilter(
DataTimeServiceImpl.BROADCAST_ACTION));
}
@Override
public void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(svcConn);
}
private void updateForecast() {
try {
String page = service.getForecastPage();
if (page != null) {
browser.loadDataWithBaseURL(null, page, "text/html", "UTF-8",
null);
browser.postDelayed(new Runnable() {
public void run() {
updateForecast();
}
}, 1000);
}
} catch (final Throwable t) {
svcConn.onServiceDisconnected(null);
}
}
//接收者实例
private BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
runOnUiThread(new Runnable() {
public void run() {
updateForecast();
}
});
}
};
}
更多精彩
赞助商链接