当前位置:首页 » 安卓系统 » 安卓线程回调是什么意思

安卓线程回调是什么意思

发布时间: 2022-04-29 19:38:03

① 如何在android的jni线程中实现回调

JNI回调是指在c/c++代码中调用java函数,当在c/c++的线程中执行回调函数时,会导致回调失败。 其中一种在Android系统的解决方案是: 把c/c++中所有线程的创建,由pthread_create函数替换为由Java层的创建线程的函数AndroidRuntime::createJavaThread。 假设有c++函数: [cpp] view plain void *thread_entry(void *args) { while(1) { printf("thread running...\n"); sleep(1); } } void init() { pthread_t thread; pthread_create(&thread,NULL,thread_entry,(void *)NULL); } init()函数创建一个线程,需要在该线程中调用java类Test的回调函数Receive: [cpp] view plain public void Receive(char buffer[],int length){ String msg = new String(buffer); msg = "received from jni callback:" + msg; Log.d("Test", msg); } 首先在c++中定义回调函数指针: [cpp] view plain //test.h #include <pthread.h> //function type for receiving data from native typedef void (*ReceiveCallback)(unsigned char *buf, int len); /** Callback for creating a thread that can call into the Java framework code. * This must be used to create any threads that report events up to the framework. */ typedef pthread_t (* CreateThreadCallback)(const char* name, void (*start)(void *), void* arg); typedef struct{ ReceiveCallback recv_cb; CreateThreadCallback create_thread_cb; }Callback; 再修改c++中的init和thread_entry函数: [cpp] view plain //test.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <sys/wait.h> #include <unistd.h> #include "test.h" void *thread_entry(void *args) { char *str = "i'm happy now"; Callback cb = NULL; int len; if(args != NULL){ cb = (Callback *)args; } len = strlen(str); while(1) { printf("thread running...\n"); //invoke callback method to java if(cb != NULL && cb->recv_cb != NULL){ cb->recv_cb((unsigned char*)str, len); } sleep(1); } } void init(Callback *cb) { pthread_t thread; //pthread_create(&thread,NULL,thread_entry,(void *)NULL); if(cb != NULL && cb->create_thread_cb != NULL) { cb->create_thread_cb("thread",thread_entry,(void *)cb); } } 然后在jni中实现回调函数,以及其他实现: [cpp] view plain //jni_test.c #include <stdlib.h> #include <malloc.h> #include <jni.h> #include <JNIHelp.h> #include "android_runtime/AndroidRuntime.h" #include "test.h" #define RADIO_PROVIDER_CLASS_NAME "com/tonny/Test" using namespace android; static jobject mCallbacksObj = NULL; static jmethodID method_receive; static void (JNIEnv* env, const char* methodName) { if (env->ExceptionCheck()) { LOGE("An exception was thrown by callback '%s'.", methodName); LOGE_EX(env); env->ExceptionClear(); } } static void receive_callback(unsigned char *buf, int len) { int i; JNIEnv* env = AndroidRuntime::getJNIEnv(); jcharArray array = env->NewCharArray(len); jchar *pArray ; if(array == NULL){ LOGE("receive_callback: NewCharArray error."); return; } pArray = (jchar*)calloc(len, sizeof(jchar)); if(pArray == NULL){ LOGE("receive_callback: calloc error."); return; } // buffer to jchar array for(i = 0; i < len; i++) { *(pArray + i) = *(buf + i); } // buffer to jcharArray env->SetCharArrayRegion(array,0,len,pArray); //invoke java callback method env->CallVoidMethod(mCallbacksObj, method_receive,array,len); //release resource env->DeleteLocalRef(array); free(pArray); pArray = NULL; (env, __FUNCTION__); } static pthread_t create_thread_callback(const char* name, void (*start)(void *), void* arg) { return (pthread_t)AndroidRuntime::createJavaThread(name, start, arg); } static Callback mCallbacks = { receive_callback, create_thread_callback }; static void jni_class_init_native (JNIEnv* env, jclass clazz) { method_receive = env->GetMethodID(clazz, "Receive", "([CI)V"); } static int jni_init (JNIEnv *env, jobject obj) { if (!mCallbacksObj) mCallbacksObj = env->NewGlobalRef(obj); return init(&mCallbacks); } static const JNINativeMethod gMethods[] = { { "class_init_native", "()V", (void *)jni_class_init_native }, { "native_init", "()I", (void *)jni_init }, }; static int registerMethods(JNIEnv* env) { const char* const kClassName = RADIO_PROVIDER_CLASS_NAME; jclass clazz; /* look up the class */ clazz = env->FindClass(kClassName); if (clazz == NULL) { LOGE("Can't find class %s/n", kClassName); return -1; } /* register all the methods */ if (env->RegisterNatives(clazz,gMethods,sizeof(gMethods)/sizeof(gMethods[0])) != JNI_OK) { LOGE("Failed registering methods for %s/n", kClassName); return -1; } /* fill out the rest of the ID cache */ return 0; } jint JNI_OnLoad(JavaVM* vm, void* reserved) { JNIEnv* env = NULL; jint result = -1; LOGI("Radio JNI_OnLoad"); if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) { LOGE("ERROR: GetEnv failed/n"); goto fail; } if(env == NULL){ goto fail; } if (registerMethods(env) != 0) { LOGE("ERROR: PlatformLibrary native registration failed/n"); goto fail; } /* success -- return valid version number */ result = JNI_VERSION_1_4; fail: return result; } jni的Android.mk文件中共享库设置为: [cpp] view plain LOCAL_SHARED_LIBRARIES := liblog libcutils libandroid_runtime libnativehelper 最后再实现Java中的Test类: [java] view plain //com.tonny.Test.java public class Test { static{ try { System.loadLibrary("test"); class_init_native(); } catch(UnsatisfiedLinkError ule){ System.err.println("WARNING: Could not load library libtest.so!"); } } public int initialize() { return native_radio_init(); } public void Receive(char buffer[],int length){ String msg = new String(buffer); msg = "received from jni callback" + msg; Log.d("Test", msg); } protected static native void class_init_native(); protected native int native_init(); }

② Android 如何监听一个线程的开始和结束

方法一:轮询

比如主线程要等子线程在得到变量“val”值的时候开始用“val”的值来进行工作,这个比较简单。

方法二,回调
回调就是调用别的对象的方法时把“自己”传进去,然后别的对象在某个时候调用“自己的方法”

publicinterfaceThreadCallback{
voidthreadStartLisener();
voidthreadEndLisener();
}{
;
publicSubRunnable(ThreadCallbackthreadCallback){
this.mThreadCallback=threadCallback;
}
@Override
publicvoidrun(){
mThreadCallback.threadStartLisener();
for(inti=0;i<5;i++){
System.out.println(Thread.currentThread().getName()+"dosomething"+i);
}
mThreadCallback.threadEndLisener();
}

}{
=newThreadCallbackTest();
publicstaticvoidmain(String[]args){
System.out.println(Thread.currentThread().getName()+"开始");
newThread(newSubRunnable(mThreadCallbackTest)).start();
}

@Override
publicvoidthreadStartLisener(){
System.out.println(Thread.currentThread().getName()+"线程,知道SubRunnable线程开始执行任务了");
}

@Override
publicvoidthreadEndLisener(){
System.out.println(Thread.currentThread().getName()+"线程,知道SubRunnable线程任务执行结束了");
}
}

③ 如何在android的jni线程中实现回调

JNI回调是指在c/c++代码中调用java函数,当在c/c++的线程中执行回调函数时,会导致回调失败。

其中一种在Android系统的解决方案是:

把c/c++中所有线程的创建,由pthread_create函数替换为由Java层的创建线程的函数AndroidRuntime::createJavaThread。

假设有c++函数:

[cpp] view plain
void *thread_entry(void *args)
{
while(1)
{
printf("thread running...\n");
sleep(1);
}

}

void init()
{
pthread_t thread;
pthread_create(&thread,NULL,thread_entry,(void *)NULL);
}

init()函数创建一个线程,需要在该线程中调用java类Test的回调函数Receive:

[cpp] view plain
public void Receive(char buffer[],int length){
String msg = new String(buffer);
msg = "received from jni callback:" + msg;
Log.d("Test", msg);
}

首先在c++中定义回调函数指针:

[cpp] view plain
//test.h
#include <pthread.h>
//function type for receiving data from native
typedef void (*ReceiveCallback)(unsigned char *buf, int len);

/** Callback for creating a thread that can call into the Java framework code.
* This must be used to create any threads that report events up to the framework.
*/
typedef pthread_t (* CreateThreadCallback)(const char* name, void (*start)(void *), void* arg);

typedef struct{
ReceiveCallback recv_cb;
CreateThreadCallback create_thread_cb;
}Callback;

再修改c++中的init和thread_entry函数:

[cpp] view plain
//test.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/wait.h>
#include <unistd.h>
#include "test.h"

void *thread_entry(void *args)
{
char *str = "i'm happy now";
Callback cb = NULL;
int len;
if(args != NULL){
cb = (Callback *)args;
}

len = strlen(str);
while(1)
{
printf("thread running...\n");
//invoke callback method to java
if(cb != NULL && cb->recv_cb != NULL){
cb->recv_cb((unsigned char*)str, len);
}
sleep(1);
}

}

void init(Callback *cb)
{
pthread_t thread;
//pthread_create(&thread,NULL,thread_entry,(void *)NULL);
if(cb != NULL && cb->create_thread_cb != NULL)
{
cb->create_thread_cb("thread",thread_entry,(void *)cb);
}
}

然后在jni中实现回调函数,以及其他实现:

[cpp] view plain
//jni_test.c
#include <stdlib.h>
#include <malloc.h>
#include <jni.h>
#include <JNIHelp.h>
#include "android_runtime/AndroidRuntime.h"

#include "test.h"
#define RADIO_PROVIDER_CLASS_NAME "com/tonny/Test"

using namespace android;

static jobject mCallbacksObj = NULL;
static jmethodID method_receive;

static void (JNIEnv* env, const char* methodName) {
if (env->ExceptionCheck()) {
LOGE("An exception was thrown by callback '%s'.", methodName);
LOGE_EX(env);
env->ExceptionClear();
}
}

static void receive_callback(unsigned char *buf, int len)
{
int i;
JNIEnv* env = AndroidRuntime::getJNIEnv();
jcharArray array = env->NewCharArray(len);
jchar *pArray ;

if(array == NULL){
LOGE("receive_callback: NewCharArray error.");
return;
}

pArray = (jchar*)calloc(len, sizeof(jchar));
if(pArray == NULL){
LOGE("receive_callback: calloc error.");
return;
}

// buffer to jchar array
for(i = 0; i < len; i++)
{
*(pArray + i) = *(buf + i);
}
// buffer to jcharArray
env->SetCharArrayRegion(array,0,len,pArray);
//invoke java callback method
env->CallVoidMethod(mCallbacksObj, method_receive,array,len);
//release resource
env->DeleteLocalRef(array);
free(pArray);
pArray = NULL;

(env, __FUNCTION__);
}

static pthread_t create_thread_callback(const char* name, void (*start)(void *), void* arg)
{
return (pthread_t)AndroidRuntime::createJavaThread(name, start, arg);
}

static Callback mCallbacks = {
receive_callback,
create_thread_callback
};

static void jni_class_init_native
(JNIEnv* env, jclass clazz)
{
method_receive = env->GetMethodID(clazz, "Receive", "([CI)V");
}

static int jni_init
(JNIEnv *env, jobject obj)
{

if (!mCallbacksObj)
mCallbacksObj = env->NewGlobalRef(obj);

return init(&mCallbacks);
}

static const JNINativeMethod gMethods[] = {
{ "class_init_native", "()V", (void *)jni_class_init_native },
{ "native_init", "()I", (void *)jni_init },
};

static int registerMethods(JNIEnv* env) {

const char* const kClassName = RADIO_PROVIDER_CLASS_NAME;
jclass clazz;
/* look up the class */
clazz = env->FindClass(kClassName);
if (clazz == NULL) {
LOGE("Can't find class %s/n", kClassName);
return -1;
}
/* register all the methods */
if (env->RegisterNatives(clazz,gMethods,sizeof(gMethods)/sizeof(gMethods[0])) != JNI_OK)
{
LOGE("Failed registering methods for %s/n", kClassName);
return -1;
}
/* fill out the rest of the ID cache */
return 0;
}

jint JNI_OnLoad(JavaVM* vm, void* reserved) {
JNIEnv* env = NULL;
jint result = -1;
LOGI("Radio JNI_OnLoad");
if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
LOGE("ERROR: GetEnv failed/n");
goto fail;
}

if(env == NULL){
goto fail;
}
if (registerMethods(env) != 0) {
LOGE("ERROR: PlatformLibrary native registration failed/n");
goto fail;
}
/* success -- return valid version number */
result = JNI_VERSION_1_4;
fail:
return result;
}

jni的Android.mk文件中共享库设置为:

[cpp] view plain
LOCAL_SHARED_LIBRARIES := liblog libcutils libandroid_runtime libnativehelper

最后再实现Java中的Test类:

[java] view plain
//com.tonny.Test.java

public class Test {

static{
try {
System.loadLibrary("test");
class_init_native();

} catch(UnsatisfiedLinkError ule){
System.err.println("WARNING: Could not load library libtest.so!");
}

}

public int initialize() {
return native_radio_init();
}

public void Receive(char buffer[],int length){
String msg = new String(buffer);
msg = "received from jni callback" + msg;
Log.d("Test", msg);
}

protected static native void class_init_native();

protected native int native_init();

}

④ 多线程里怎么实现注册回调机制

在Android中到处可见接口回调机制,尤其是UI事件处理方面,这里介绍android接口回调机制,涉及到android接口回调相关知识

在使用接口回调的时候发现了一个经常犯的错误,就是回调函数里面的实现有可能是用多线程或者是异步任务去做的,这就会导致咱们期望函数回调完毕去返回一个主函数的结果,实际发现是行不通的,因为如果回调是多线程的话是无法和主函数同步的,也就是返回的数据是错误的,这是非常隐秘的一个错误。那有什么好的方法去实现数据的线性传递呢?先介绍下回调机制原理。
回调函数
回调函数就是一个通过函数指针调用的函数。如果把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用为调用它所指向的函数时,咱们就说这是回调函数。回调函数不是由该函数的实现方直接调用,而是在特定的事件或条件发生时由另外的一方调用的,用于对该事件或条件进行响应。
开发中,接口回调是经常用到的。
接口回调的意思即,注册之后并不立马执行,而在某个时机触发执行。

⑤ 如何在android的jni线程中实现回调

jni回调是指在c/c++代码中调用java函数,当在c/c++的线程中执行回调函数时,会导致回调失败。

其中一种在Android系统的解决方案是:

把c/c++中所有线程的创建,由pthread_create函数替换为由Java层的创建线程的函数AndroidRuntime::createJavaThread。

假设有c++函数:

[cpp] view plain
void *thread_entry(void *args)
{
while(1)
{
printf("thread running...\n");
sleep(1);
}

}

void init()
{
pthread_t thread;
pthread_create(&thread,NULL,thread_entry,(void *)NULL);
}

init()函数创建一个线程,需要在该线程中调用java类Test的回调函数Receive:

[cpp] view plain
public void Receive(char buffer[],int length){
String msg = new String(buffer);
msg = "received from jni callback:" + msg;
Log.d("Test", msg);
}

首先在c++中定义回调函数指针:

[cpp] view plain
//test.h
#include <pthread.h>
//function type for receiving data from native
typedef void (*ReceiveCallback)(unsigned char *buf, int len);

/** Callback for creating a thread that can call into the Java framework code.
* This must be used to create any threads that report events up to the framework.
*/
typedef pthread_t (* CreateThreadCallback)(const char* name, void (*start)(void *), void* arg);

typedef struct{
ReceiveCallback recv_cb;
CreateThreadCallback create_thread_cb;
}Callback;

再修改c++中的init和thread_entry函数:

[cpp] view plain
//test.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/wait.h>
#include <unistd.h>
#include "test.h"

void *thread_entry(void *args)
{
char *str = "i'm happy now";
Callback cb = NULL;
int len;
if(args != NULL){
cb = (Callback *)args;
}

len = strlen(str);
while(1)
{
printf("thread running...\n");
//invoke callback method to java
if(cb != NULL && cb->recv_cb != NULL){
cb->recv_cb((unsigned char*)str, len);
}
sleep(1);
}

}

void init(Callback *cb)
{
pthread_t thread;
//pthread_create(&thread,NULL,thread_entry,(void *)NULL);
if(cb != NULL && cb->create_thread_cb != NULL)
{
cb->create_thread_cb("thread",thread_entry,(void *)cb);
}
}

然后在jni中实现回调函数,以及其他实现:

[cpp] view plain
//jni_test.c
#include <stdlib.h>
#include <malloc.h>
#include <jni.h>
#include <JNIHelp.h>
#include "android_runtime/AndroidRuntime.h"

#include "test.h"
#define RADIO_PROVIDER_CLASS_NAME "com/tonny/Test"

using namespace android;

static jobject mCallbacksObj = NULL;
static jmethodID method_receive;

static void (JNIEnv* env, const char* methodName) {
if (env->ExceptionCheck()) {
LOGE("An exception was thrown by callback '%s'.", methodName);
LOGE_EX(env);
env->ExceptionClear();
}
}

static void receive_callback(unsigned char *buf, int len)
{
int i;
JNIEnv* env = AndroidRuntime::getJNIEnv();
jcharArray array = env->NewCharArray(len);
jchar *pArray ;

if(array == NULL){
LOGE("receive_callback: NewCharArray error.");
return;
}

pArray = (jchar*)calloc(len, sizeof(jchar));
if(pArray == NULL){
LOGE("receive_callback: calloc error.");
return;
}

// buffer to jchar array
for(i = 0; i < len; i++)
{
*(pArray + i) = *(buf + i);
}
// buffer to jcharArray
env->SetCharArrayRegion(array,0,len,pArray);
//invoke java callback method
env->CallVoidMethod(mCallbacksObj, method_receive,array,len);
//release resource
env->DeleteLocalRef(array);
free(pArray);
pArray = NULL;

(env, __FUNCTION__);
}

static pthread_t create_thread_callback(const char* name, void (*start)(void *), void* arg)
{
return (pthread_t)AndroidRuntime::createJavaThread(name, start, arg);
}

static Callback mCallbacks = {
receive_callback,
create_thread_callback
};

static void jni_class_init_native
(JNIEnv* env, jclass clazz)
{
method_receive = env->GetMethodID(clazz, "Receive", "([CI)V");
}

static int jni_init
(JNIEnv *env, jobject obj)
{

if (!mCallbacksObj)
mCallbacksObj = env->NewGlobalRef(obj);

return init(&mCallbacks);
}

static const JNINativeMethod gMethods[] = {
{ "class_init_native", "()V", (void *)jni_class_init_native },
{ "native_init", "()I", (void *)jni_init },
};

static int registerMethods(JNIEnv* env) {

const char* const kClassName = RADIO_PROVIDER_CLASS_NAME;
jclass clazz;
/* look up the class */
clazz = env->FindClass(kClassName);
if (clazz == NULL) {
LOGE("Can't find class %s/n", kClassName);
return -1;
}
/* register all the methods */
if (env->RegisterNatives(clazz,gMethods,sizeof(gMethods)/sizeof(gMethods[0])) != JNI_OK)
{
LOGE("Failed registering methods for %s/n", kClassName);
return -1;
}
/* fill out the rest of the ID cache */
return 0;
}

jint JNI_OnLoad(JavaVM* vm, void* reserved) {
JNIEnv* env = NULL;
jint result = -1;
LOGI("Radio JNI_OnLoad");
if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
LOGE("ERROR: GetEnv failed/n");
goto fail;
}

if(env == NULL){
goto fail;
}
if (registerMethods(env) != 0) {
LOGE("ERROR: PlatformLibrary native registration failed/n");
goto fail;
}
/* success -- return valid version number */
result = JNI_VERSION_1_4;
fail:
return result;
}

jni的Android.mk文件中共享库设置为:

[cpp] view plain
LOCAL_SHARED_LIBRARIES := liblog libcutils libandroid_runtime libnativehelper

最后再实现Java中的Test类:

[java] view plain
//com.tonny.Test.java

public class Test {

static{
try {
System.loadLibrary("test");
class_init_native();

} catch(UnsatisfiedLinkError ule){
System.err.println("WARNING: Could not load library libtest.so!");
}

}

public int initialize() {
return native_radio_init();
}

public void Receive(char buffer[],int length){
String msg = new String(buffer);
msg = "received from jni callback" + msg;
Log.d("Test", msg);
}

protected static native void class_init_native();

protected native int native_init();

}

⑥ Android创建子线程和回调主线程的几种方式

在一个Android 程序开始运行的时候,会单独启动一个Process。默认的情况下,所有这个程序中的Activity或者Service(Service和 Activity只是Android提供的Components中的两种,除此之外还有Content Provider和Broadcast Receiver)都会跑在这个Process。
一个Android 程序默认情况下也只有一个Process,但一个Process下却可以有许多个Thread。在这么多Thread当中,有一个Thread,我们称之为UI Thread。UI Thread在Android程序运行的时候就被创建,是一个Process当中的主线程Main Thread,主要是负责控制UI界面的显示、更新和控件交互。在Android程序创建之初,一个Process呈现的是单线程模型,所有的任务都在一个线程中运行。因此,我们认为,UI Thread所执行的每一个函数,所花费的时间都应该是越短越好。而其他比较费时的工作(访问网络,下载数据,查询数据库等),都应该交由子线程去执行,以免阻塞主线程。
那么,UI Thread如何和其他Thread一起工作呢?常用方法是:
诞生一个主线程的Handler物件,当做Listener去让子线程能将讯息Push到主线程的Message Quene里,以便触发主线程的handlerMessage()函数,让主线程知道子线程的状态,并在主线程更新UI。

⑦ 如何在android的jni线程中实现回调

您好,很高兴能帮助您
如果是C/C++回调,你只要参考linux的线程指南,在线程函数中传入回调函数地址就行了。如果是要回调到Java层,稍微复杂点。
首先,你需要在onload的时候,找到回调函数所在的类,用全局变量保存:
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
{
LOGE("JNI_OnLoad start");
jint version;
g_vm = vm; // 全局变量保存
JNIEnv *env;
jobject cls;
version = vm->GetEnv((void **)&env, JNI_VERSION_1_2);

if (env)
{
g_clazz = env->FindClass(CLASS_CustomSurfaceView); // 全局变量保存
}

LOGE("JNI_OnLoad finish g_clazz = 0x%x", g_clazz);
return JNI_VERSION_1_2;
}

在JNI启动线程的时候,需要把线程挂到JVM上,不然不能访问Java。你有了g_vm, g_clazz, 以及env,就可以做回调操作了。
// 线程函数

void *threadFunc(void *data)
{
JNIEnv *env = MNull;
int ret = g_vm->AttachCurrentThread( (JNIEnv **) &env, MNull); // 挂到JVM
if (ret < 0)
{
LOGE("fail to attach");
return;
}
// TODO: 在这里做你的回调操作

g_vm->DetachCurrentThread(); // 从JVM卸载
return;
}

你的采纳是我前进的动力,
记得好评和采纳,答题不易,互相帮助,

⑧ Android主线程到底是什么

  • Android中关于主线程的理解:

Android的主线程是UI线程,在Android中,四大组件运行在主线程中,在主线程中做耗时操作会导致程序出现卡顿甚至出现ANR异常,一个基本常识就是将耗时操作放到子线程中去处理,然后通过Handler回调到主线程。

  • 有三点还需要注意:

  1. 因为四大组件运行在一个主线程中,那么若果当前界面在显示的时候,后台的activity仍有处理逻辑再运行的话,仍然会造成当前界面的卡顿。

  2. 通过Handler回调到主线程只是避免程序出现ANR的第一步,必须要注意handler中逻辑处理的耗时,如果将很多消息都扔给了handler,那么也会给主线程造成压力,导致程序运行卡顿。

  3. 四大组件、Handler都是在一个线程中,那么主线程在同一时刻不可能发送两个广播,换句话说就是若果能够保证所有的广播都是在主线程中发送,那么广播内部其实不需要加上对异步操作的处理。

⑨ android 网络请求成功回调是什么线程

这是一种比较常见的做法,主要是为了架构以及稳定性。
首先,一般大型软件开发时,负责网络通信的,和对数据做处理的,往往是两个不同的模块
这样通过回调的方式,使代码的耦合性降低,更易于分块。
其次,为了避免在处理数据时占用过多时间,引起网络数据的丢失及拥堵。

⑩ 在java中回调函数怎么理解,android中的回调函数和java中有区别吗

可以理解为一种逻辑的延伸。例如在java中已onXXX开头的方法。
这些方法通常是一种信息的延伸。预示着某些事件发生了。这些信息通常是某些大逻辑的一部分。而其他部分不需要开发者考虑。
比如onCreate方法,了解了生命周期就会知道他是整个activity第一个执行的代码。那么他究竟是谁调用的。这个一般不需要考虑。只需要了解系统在调用即可。 而这个方法在执行的时候。需要你去补充这个方法的实现。 所以相当于你完成了事件的余下部分。

比如早晨需要在闹铃响之后起床。那么你可以设置一个timer来记时或者是一个线程来跑。当发现到的时候。需要把这个事件抛出去。至于谁来处理。这不是闹铃考虑的范围。他的作用就是触发事件.至于处理。那么需要处理这个事件的。可以用很多方法来建立关联的句柄。
比如。通过接口的实现。继承。或者像android里的广播。

回调函数描述的是一种模式。这个和语言或平台无关。所以android中和java中是一样的。C++中也有。

热点内容
如何恢复儿童储蓄密码箱原始密码 发布:2025-05-11 18:57:10 浏览:285
javajdk区别 发布:2025-05-11 18:45:21 浏览:35
如何防止apk被反编译 发布:2025-05-11 18:45:16 浏览:152
安卓什么功能好 发布:2025-05-11 18:21:31 浏览:354
我的世界2b2t服务器中国版 发布:2025-05-11 18:16:35 浏览:693
萌将风云脚本 发布:2025-05-11 18:07:14 浏览:745
密码锁aid代表什么 发布:2025-05-11 18:00:01 浏览:757
编程的组成 发布:2025-05-11 17:58:34 浏览:808
火山易语言apk反编译 发布:2025-05-11 17:52:01 浏览:814
钢琴密码锁本的密码该在哪里看 发布:2025-05-11 17:49:44 浏览:469