×

onreceive 方法 c

onreceive(的 onReceive事件能调用子页面的方法吗)

admin admin 发表于2023-11-09 05:55:51 浏览47 评论0

抢沙发发表评论

本文目录

的 onReceive事件能调用子页面的方法吗

两种方式:1、子页面把自己的model传回,model代码里this就是model2、主的onReceive事件中event.sender是子页面的WindowReceiver对象,通过这个组件的getModel()可以获取子的model

请问,关于安卓广播接收者的问题,安卓中onReceive方法中的参数context和intent

context是上下文环境,一般是说明你这个在哪个包里面进行的活动还有加载资源值也要用到,你可以先记住有这么一个东西,intent用于页面跳转可指定包名显式跳转,也可以设定参数进行隐式跳转,多用用就知道了,建议自己手打不要光看书

Android怎么给onReceive()函数传参数

Android怎么给onReceive()函数传参数有几种解决办法解决方案1:该函数是系统回调函数,不是给你调用的,你需要调用sendBroadcast(Intent o)解决方案2:这是个广播接收器,用来接收广播的,可以通过intent来获取广播传过来的值解决方案3:解决方案4:发送广播消息是通过Context.sendBroadcast(intent)intent中附加要传递的值

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

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

为什么onReceive不会执行呢

代码好像是没什么问题,不过可以修改一下:public class Test extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF); registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { System.out.println(“Screen off...................“); } }, filter);}@Overrideprotected void onResume() { super.onResume(); System.out.println(“onResume()“);}@Overrideprotected void onDestroy() { super.onDestroy(); System.out.println(“onDestroy()“);}@Overrideprotected void onPause() { super.onPause(); System.out.println(“onPause()“);}

onReceive(Context context, Intent intent)方法的两个参数是什么意思

发送广播时:Intent intent = new Intent(BROADCAST_COUNTER_ACTION); intent.putExtra(COUNTER_VALUE, counter); context.sendBroadcast(intent); onReceive(Context context, Intent intent)的context与intent就是发送广播时的这个context和intent

请问CSocket 类中的 OnReceive()的参数

CAsyncSocket::OnReceiveCalled by the framework to notify this socket that there is data in the buffer that can be retrieved by calling the Receive member function.virtual void OnReceive( int nErrorCode );ParametersnErrorCode 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. RemarksFor more information, see Windows Sockets: Socket Notifications.Examplevoid 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 = 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);}由以上可知, 这个参数是输出参数,如果函数成功接收数据,则该参数为0,否则输出为错误代码。能否讲一下什么是函数的输出参数?////////////////////////////////如果说这样的函数:void add(int & value){ value++;}int n = 5;add(n);n就变成6了。///////////////////////////////////////我看错了,呵呵,不是输出参数,因为函数原型是:virtual void OnReceive( int nErrorCode );nErrorCode是int,不是int &或者int *,所以不是输出参数。我查看了一下MSDN和CAsyncSocket的源码://///////////////////////////////////////////////////////////////////////////// CAsyncSocket Overridable callbacksvoid CAsyncSocket::OnReceive(int /*nErrorCode*/){}这个函数什么都没做。我估计当框架接收到数据到来的消息后,就调用CAsyncSocket::OnReceive,而nErrorCode这个参数也是框架自己提供的,表示当前网络的状态。程序员不用管它,只须在自己的socket类中重载该函数,并在函数体内实现自己的接收代码就可以了。