您的位置首页百科知识

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

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 config = loadArray();

for(int i=0;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 apply to the OnReceive member function:

0 The function executed successfully.

WSAENETDOWN The Windows Sockets implementation detected that the network subsystem failed.

Remarks

Called by the framework to notify this socket that there is data in the buffer that can be retrieved by calling the Receive member function. For more information, see the articleWindows Sockets: Socket Notifications in Visual C++ Programmer's Guide.

Example

void CMyAsyncSocket::OnReceive(int nErrorCode) // CMyAsyncSocket is

// derived from CAsyncSocket

{

static int i=0;

i++;

TCHAR buff;

int nRead;

nRead = Receive(buff, 4096);

switch (nRead)

{

case 0:

Close();

break;

case SOCKET_ERROR:

if (GetLastError() != WSAEWOULDBLOCK)

{

AfxMessageBox ("Error occurred");

Close();

}

break;

default:

buff[nRead] = 0; //terminate the string

CString szTemp(buff);

m_strRecv += szTemp; // m_strRecv is a CString declared

// in CMyAsyncSocket

if (szTemp.CompareNoCase("bye") == 0 ) ShutDown();

}

CAsyncSocket::OnReceive(nErrorCode);

}

抖音看短剧