也是郁闷了好些天才搞定
场景:如题,activity (页面)操作后返回数据到,插件类里面。
不说了,直接上代码:
插件类:
package com.example.chajian;
import android.content.Intent;
import android.util.Log;
import org.json.JSONArray;
import io.dcloud.common.DHInterface.IApp;
import io.dcloud.common.DHInterface.ISysEventListener;
import io.dcloud.common.DHInterface.IWebview;
import io.dcloud.common.DHInterface.StandardFeature;
import io.dcloud.common.util.JSUtil;
public class Ceshi extends StandardFeature {
public void Test(IWebview pWebview, JSONArray array) {
final IWebview iWebview = pWebview;
final String CallBackID = array.optString(0);
final JSONArray newArray = new JSONArray();
newArray.put(array.optString(1));
newArray.put(array.optString(2));
newArray.put(array.optString(3));
newArray.put(array.optString(4));
//添加监听
final IApp _app = pWebview.obtainFrameView().obtainApp();
_app.registerSysEventListener(new ISysEventListener() {
@Override
public boolean onExecute(SysEventType pEventType, Object pArgs) {
Object[] _args = (Object[]) pArgs;
int requestCode = (Integer) _args[0];
int resultCode = (Integer) _args[1];
Intent data = (Intent) _args[2];
//[Ljava.lang.Object;@e89a7c8
Log.d("aaa", "onExecute: _args " + _args);
//1
Log.d("aaa", "onExecute: requestCode " + requestCode);
//-1
Log.d("aaa", "onExecute: resultCode " + resultCode);
//Intent { (has extras) }
Log.d("aaa", "onExecute: data " + data);
if (pEventType == SysEventType.onActivityResult) {
//用完给取消注册监听
_app.unregisterSysEventListener(this, SysEventType.onActivityResult);
//判断请求码
if (requestCode == 1) {
//获取返回值
String returnData = data.getStringExtra("return_data");
//执行 js 回调
JSUtil.execCallback(iWebview, CallBackID, returnData, JSUtil.OK, false);
Log.d("aaa", "返回的数据为:" + returnData);
}
}
return false;
}
}, SysEventType.onActivityResult);
//打开 activity
Intent openItent = new Intent(pWebview.getActivity(), Open.class);
// Intent openItent = new Intent(pWebview.getContext(),Open.class);
pWebview.getActivity().startActivityForResult(openItent, 1);
}
}
activity 类:
package com.example.chajian;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class Open extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.open_activity);
Button open = findViewById(R.id.open);
open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("aaa", "Open.onCreate : open");
Intent intent = new Intent();
intent.putExtra("return_data","我去它妹的!");
setResult(RESULT_OK,intent);
finish();
}
});
}
}
代码并不复杂,一行一行的看,很好理解,还有注释。
逻辑是,插件类设置事件监听-打开 activity-activity 设置返回值后 finish() (关闭)-插件类监听到返回值处理执行 js 回调。
5 个评论
要回复文章请先登录或注册
note744917
4***@qq.com
4***@qq.com
2***@qq.com (作者)
4***@qq.com