OnReceive()怎么用,应写在那里?

来源:百度知道 编辑:UC知道 时间:2024/07/07 11:24:53
OnReceive()怎么用,应写在那里?

首先Service、Broadcast Receiver、Content Provider都是运行在UI线程,或者说主线程中的,就是启动一个Activity时开始运行的那个进程。但是Broadcast Receiver不像Service,生命周期较短,耗时操作你不能在onReceive中执行的,比如你试试在其中启动一个对话框看看,会报错的。可能是这个原因导致你加上延迟后没有更新文字。还有,要加上执行完调用timer.cancel();结束任务。如果简单的想五秒后执行,直接用Thread,在run方法中sleep(5000);就可以了。代码如下:
new Thread(){

@Override
public void run() {
super.run();
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String text="";
List<String> config = loadArray();
for(int i=0;i<config.size();i++){
//am.killBackgroundProcesses(config.get(i));
text+=config.get(i)+"\n\r";
}
text+="off";
tv_show.setText(text);
}

}.start();
直接加到onReceive方法中就行了。

CAsyncSocket::OnReceive
virtual void OnReceive( int nErrorCode );

Parameters

nErrorCode

The most recent error on a socket. The following error codes appl