我在一个工作流程中遇到问题。
我有混合应用程序。我正在调用一个 java 函数,该函数以打开、 Activity 和返回的方式进行。我希望将activity的返回值返回给javascript。但这里发生的是函数在 Activity 完成之前返回。我尝试使用线程,但它停止渲染用户界面。请帮忙...... #
主要 Activity (Ftsbeta.java)
/* 包和导入语句 */
public class Ftsbeta extends CordovaActivity {
String retURL="";
public static Thread t;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
t=Thread.currentThread();
appView.addJavascriptInterface(this, "MainActivity");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.loadUrl(Config.getStartUrl());
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@JavascriptInterface
public String customFunctionCalled(final String url) throws InterruptedException {
Log.e("Custom Function Called", "Custom Function Called");
t= new Thread(new Runnable() {
@Override public void run()
{
// do some work here
synchronized (t) {
try {
Intent i = new Intent(Ftsbeta.this, CustomWV.class);
i.putExtra("url", url);
startActivityForResult(i, 02);
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e("msg","Exception");
e.printStackTrace();
}
}
}
});
t.start();
// Thread.currentThread().join();
Log.e("msg","returned");
return retURL;
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
// TODO Auto-generated method stub
// check if the request code is same as what is passed here it is 2
synchronized (t) {
if (requestCode == 2) {
// fetch the message String
if(resultCode==RESULT_OK){
retURL = intent.getStringExtra("url");
// Set the message string in textView
Log.e("url",retURL);
}
}
Log.e("here","time to notify");
t.notify();
}
}
}
<强># 另一个正在打开的 Activity (Customwv.java) /* 进口和包装 */
public class CustomWV extends Activity {
private WebView webView;
ProgressDialog d;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom);
Intent myIntent = getIntent(); // gets the previously created intent
String loadURL = myIntent.getStringExtra("url"); // will return
Log.e("here","opening windows");
webView = (WebView) findViewById(R.id.wv1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.e("here","opening windows2");
d = ProgressDialog.show(CustomWV.this, "Fts",
"Please Wait While Till Site Loads");
view.loadUrl(url);
return false;
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
d.dismiss();
if (url.contains("action=done")) {
Intent intentMessage = new Intent();
// put the message to return as result in Intent
intentMessage.putExtra("url", url);
// Set The Result in Intent
setResult(02, intentMessage);
finish();
}
}
});
webView.loadUrl(loadURL);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
return super.onCreateOptionsMenu(menu);
}
}
来 self 调用它的地方的Javascript。
代码是: var a= window.MainActivity.customFunctionCalled(pgurl);
请帮助我以正确的方式做到这一点。
提前致谢...
最佳答案
Cordova插件机制的全部目的是建立native和js端之间的通信。查看文档和 echo 插件示例。
使用插件机制等待和延迟响应等是可能的。 github 中还有几个插件可以处理您指出的具体问题。
关于javascript - 调用一个打开 Activity 并将 Activity 结果返回给 JavaScript 的 Java 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25519327/