0
我正在嘗試將mobfox廣告添加到簡單的webview應用中,並且我遵循了他們的指南。但我無法弄清楚,尤其是MainActivity.java部分。獲取不同的錯誤,如類,接口或枚舉的期望等。有人可以告訴我如何將mobfox代碼添加到java文件?嘗試了很多,但我無法使它工作。使用Android工作室。提前致謝。在活動中添加Mobfox橫幅廣告代碼
MainActivity.java代碼
package com.webapp.webviewapp1;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends ActionBarActivity {
private WebView view; //membuat variabel view agar bisa akses method onKeyDown
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new MyBrowser());
view.loadUrl("http://www.google.com"); //try js alert
view.setWebChromeClient(new WebChromeClient()); // adding js alert support
}
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
//ketika disentuh tombol back
if ((keyCode == KeyEvent.KEYCODE_BACK) && view.canGoBack()) {
view.goBack(); //method goback() dieksekusi untuk kembali pada halaman sebelumnya
return true;
}
// Jika tidak ada history (Halaman yang sebelumnya dibuka)
// maka akan keluar dari activity
return super.onKeyDown(keyCode, event);
}
}
的MobFox橫幅廣告代碼
// ...
import com.mobfox.sdk.bannerads.Banner;
import com.mobfox.sdk.bannerads.BannerListener;
// ...
Banner banner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
banner = (Banner) findViewById(R.id.banner);
final Activity self = this;
banner.setListener(new BannerListener() {
@Override
public void onBannerError(View banner, Exception e) {
Toast.makeText(self, e.getMessage(), Toast.LENGTH_SHORT).show();
}
@Override
public void onBannerLoaded(View banner) {
Toast.makeText(self, "loaded", Toast.LENGTH_SHORT).show();
}
@Override
public void onBannerClosed(View banner) {
Toast.makeText(self, "closed", Toast.LENGTH_SHORT).show();
}
@Override
public void onBannerFinished() {
Toast.makeText(self, "finished", Toast.LENGTH_SHORT).show();
}
@Override
public void onBannerClicked(View banner) {
Toast.makeText(self, "clicked", Toast.LENGTH_SHORT).show();
}
@Override
public void onNoFill(View banner) {
Toast.makeText(self, "no fill", Toast.LENGTH_SHORT).show();
}
});
banner.setInventoryHash("<your-publication-hash>");
banner.load();
}
//permission dialog for marshmello and above
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
banner.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
//add this so video ads will work properly
@Override
protected void onPause() {
super.onPause();
banner.onPause();
}
@Override
protected void onResume() {
super.onResume();
banner.onResume();
}
// ...
提供您main_activity.xml。 將您的INVENTORY_HASH添加到banner.setInventoryHash中,或者放入測試哈希。 爲logcat提供錯誤。 Plus提供您自己完成的工作,因爲集成非常明顯。 –