简体   繁体   中英

Adding Mobfox banner ad code in activity

I'm trying to add mobfox ads to a simple webview app and I followed their guide. But I'm unable to figure it out, especially the MainActivity.java part. Getting different errors like class,interface or enum expected etc. Can somebody tell me how to add the mobfox code into the java file? Tried lot but I'm unable to make it work. Using Android studio. Thanks in advance.

MainActivity.java Code

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 banner ad code

// ...

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();
}

// ...

Though I'm not on the Tech-Support team ,I'm pretty familiar with the integration process.

As Stanislav mentioned, it seems like you forgot to add your publication-hash to the code on:

banner.setInventoryHash("<your-publication-hash>"); // replace the content inside the parenthesis with your hash

Also, make sure your module build.gradle and .xml file are set correctly and that you got all the required permissions on AndroidManifest.xml. Please look in the official instructions again and make sure you didn't miss any step -

https://github.com/mobfox/MobFox-Android-SDK#mobfox-android-sdk-core-lib

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM