我为第 3 方广告商 (TrialPay) 创建了一个原生扩展 (Android),它需要加载 webview 并进一步在原生浏览器中打开 url。之后,它要求点击后退按钮,以便用户可以从本机浏览器返回应用程序,但此时应用程序崩溃......
这是我在本机扩展代码中的活动:
public class TrialPayOfferWallActivity extends Activity{
Button doneButton = null;
WebView offerWallWebView = null;
public static FREContext freContext;
@SuppressLint({ "NewApi", "SetJavaScriptEnabled" })
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Log.i(this.getClass().getSimpleName(), "Inside TrialPayOfferWallActivity OnCreate function");
this.setContentView(freContext.getResourceId("layout.trialpayofferwallactivity"));
Log.i(this.getClass().getSimpleName(), "1");
Bundle extraBundleObj = this.getIntent().getExtras();
Log.i(this.getClass().getSimpleName(), "2");
String urlString = extraBundleObj.getString("urlString");
Log.i(this.getClass().getSimpleName(), "3-"+urlString);
doneButton = (Button) this.findViewById(freContext.getResourceId("id.btnDone"));
Log.i(this.getClass().getSimpleName(), "4");
offerWallWebView = (WebView) this.findViewById(freContext.getResourceId("id.offerwallwebview"));
Log.i(this.getClass().getSimpleName(), "5");
doneButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setResult(RESULT_OK);
finish();
}
});
Log.i(this.getClass().getSimpleName(), "6");
offerWallWebView.getSettings().setJavaScriptEnabled(true);
offerWallWebView.getSettings().setUseWideViewPort(true);
offerWallWebView.getSettings().setLoadWithOverviewMode(true);
offerWallWebView.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
Log.i(this.getClass().getSimpleName(), "7");
this.loadWebViewWithURL(urlString);
}
public void loadWebViewWithURL(String url2)
{
offerWallWebView.loadUrl(url2);
offerWallWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("http:") || url.startsWith("https:")) {
view.loadUrl(url);
} else {
if (url.startsWith("tpbow")) {
url = url.substring(5);
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity( intent );
}
return true;
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && (offerWallWebView.getVisibility() == WebView.VISIBLE)) {
this.goBack(keyCode, event);
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
private void goBack(int keyCode, KeyEvent event) {
if (offerWallWebView.canGoBack()) {
offerWallWebView.goBack();
} else {
super.onKeyDown(keyCode, event);
}
}
protected void onPause()
{
super.onPause();
//sendErrorBack("Cancelled");
Log.i(this.getClass().getSimpleName(), "onPause");
}
protected void onStop()
{
super.onStop();
//sendErrorBack("Cancelled");
Log.i(this.getClass().getSimpleName(), "onStop");
}
protected void onResume()
{
super.onResume();
Log.i(this.getClass().getSimpleName(), "onResume");
}
protected void onStart()
{
super.onStart();
Log.i(this.getClass().getSimpleName(), "onStart");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i(this.getClass().getSimpleName(), "Inside OnActivityResult function");
if (resultCode == RESULT_OK) {
Log.i(this.getClass().getSimpleName(), "Inside OnActivityResult function->result ok");
}
}
}
我错过了什么吗?