mirror of
https://github.com/ikun0014/lx-music-mobile.git
synced 2025-07-04 12:12:09 +08:00
优化
This commit is contained in:
parent
a879207aa9
commit
44a3a37e0d
@ -5,5 +5,6 @@ public class HandlerWhat {
|
|||||||
public static final int INIT = 99;
|
public static final int INIT = 99;
|
||||||
public static final int INIT_FAILED = 500;
|
public static final int INIT_FAILED = 500;
|
||||||
public static final int INIT_SUCCESS = 200;
|
public static final int INIT_SUCCESS = 200;
|
||||||
|
public static final int DESTROY = 98;
|
||||||
public static final int LOG = 1001;
|
public static final int LOG = 1001;
|
||||||
}
|
}
|
||||||
|
@ -41,14 +41,22 @@ public class JavaScriptThread extends HandlerThread {
|
|||||||
handler.sendMessage(handler.obtainMessage(HandlerWhat.INIT_FAILED, result));
|
handler.sendMessage(handler.obtainMessage(HandlerWhat.INIT_FAILED, result));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (message.what == HandlerWhat.INIT) return;
|
switch (message.what) {
|
||||||
if (message.what == HandlerWhat.ACTION) {
|
case HandlerWhat.INIT: break;
|
||||||
|
case HandlerWhat.ACTION: {
|
||||||
Object[] data = (Object[]) message.obj;
|
Object[] data = (Object[]) message.obj;
|
||||||
Log.d("UserApi [handler]", "handler action: " + data[0]);
|
Log.d("UserApi [handler]", "handler action: " + data[0]);
|
||||||
javaScriptExecutor.callJS((String) data[0], data[1]);
|
javaScriptExecutor.callJS((String) data[0], data[1]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
case HandlerWhat.DESTROY:
|
||||||
|
javaScriptExecutor.destroy();
|
||||||
|
javaScriptExecutor = null;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
Log.w("UserApi [handler]", "Unknown message what: " + message.what);
|
Log.w("UserApi [handler]", "Unknown message what: " + message.what);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -185,4 +185,9 @@ public class QuickJS {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void destroy () {
|
||||||
|
this.jsContext.destroy();
|
||||||
|
this.jsContext = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package cn.toside.music.mobile.userApi;
|
package cn.toside.music.mobile.userApi;
|
||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
import android.os.Message;
|
import android.os.Message;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import com.facebook.react.bridge.Arguments;
|
import com.facebook.react.bridge.Arguments;
|
||||||
@ -33,10 +34,10 @@ public class UserApiModule extends ReactContextBaseJavaModule {
|
|||||||
if (this.javaScriptThread != null) destroy();
|
if (this.javaScriptThread != null) destroy();
|
||||||
Bundle info = Arguments.toBundle(data);
|
Bundle info = Arguments.toBundle(data);
|
||||||
this.javaScriptThread = new JavaScriptThread(this.reactContext, info);
|
this.javaScriptThread = new JavaScriptThread(this.reactContext, info);
|
||||||
final JsHandler jsHandler = new JsHandler(this.reactContext.getMainLooper(), this.utilsEvent);
|
this.javaScriptThread.prepareHandler(new JsHandler(this.reactContext.getMainLooper(), this.utilsEvent));
|
||||||
this.javaScriptThread.prepareHandler(jsHandler);
|
|
||||||
this.javaScriptThread.getHandler().sendEmptyMessage(HandlerWhat.INIT);
|
this.javaScriptThread.getHandler().sendEmptyMessage(HandlerWhat.INIT);
|
||||||
this.javaScriptThread.setUncaughtExceptionHandler((thread, ex) -> {
|
this.javaScriptThread.setUncaughtExceptionHandler((thread, ex) -> {
|
||||||
|
Handler jsHandler = javaScriptThread.getHandler();
|
||||||
Message message = jsHandler.obtainMessage();
|
Message message = jsHandler.obtainMessage();
|
||||||
message.what = HandlerWhat.LOG;
|
message.what = HandlerWhat.LOG;
|
||||||
message.obj = new Object[]{"error", "Uncaught exception in JavaScriptThread: " + ex.getMessage()};
|
message.obj = new Object[]{"error", "Uncaught exception in JavaScriptThread: " + ex.getMessage()};
|
||||||
@ -50,19 +51,19 @@ public class UserApiModule extends ReactContextBaseJavaModule {
|
|||||||
public boolean sendAction(String action, String info) {
|
public boolean sendAction(String action, String info) {
|
||||||
JavaScriptThread javaScriptThread = this.javaScriptThread;
|
JavaScriptThread javaScriptThread = this.javaScriptThread;
|
||||||
if (javaScriptThread == null) return false;
|
if (javaScriptThread == null) return false;
|
||||||
Message message = javaScriptThread.getHandler().obtainMessage();
|
Handler jsHandler = javaScriptThread.getHandler();
|
||||||
|
Message message = jsHandler.obtainMessage();
|
||||||
message.what = HandlerWhat.ACTION;
|
message.what = HandlerWhat.ACTION;
|
||||||
message.obj = new Object[]{action, info};
|
message.obj = new Object[]{action, info};
|
||||||
this.javaScriptThread.getHandler().sendMessage(message);
|
jsHandler.sendMessage(message);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ReactMethod
|
@ReactMethod
|
||||||
public void destroy() {
|
public void destroy() {
|
||||||
JavaScriptThread javaScriptThread = this.javaScriptThread;
|
JavaScriptThread javaScriptThread = this.javaScriptThread;
|
||||||
if (javaScriptThread == null) {
|
if (javaScriptThread == null) return;
|
||||||
return;
|
javaScriptThread.getHandler().sendEmptyMessage(HandlerWhat.DESTROY);
|
||||||
}
|
|
||||||
javaScriptThread.stopThread();
|
javaScriptThread.stopThread();
|
||||||
this.javaScriptThread = null;
|
this.javaScriptThread = null;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user