如何将admob与dukescript集成生成apk?

时间:2015-11-19 15:59:06

标签: android admob dukescript

如何在生成 android 项目的dukescript中添加 AdMob 广告功能?

例如,假设我将android加入了Hello World example并且该应用已经发布,因此我可以添加一些ad unit个在线的网站,例如广告单元ID ca-app-pub-99999999999999999/9999999999对于横幅的广告插播广告单元ID ca-app-pub-9999999999999999/9999999998

据我所知,你不能只用javascript显示它们,你应该在库文件夹中添加一些jar并明确调用它们的一些功能。

1 个答案:

答案 0 :(得分:1)

您需要添加所需的库。有一个相关的问题如何将它们安装到本地存储库here

安装库并在android项目中引用它们后,您应该能够使用AdMob。按照此处的说明在您自己的MainActivity中wrap the DukeScript MainActivity。然后,您应该可以在onCreate方法as described here中创建广告。

    import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

public class AndroidMain extends Activity {

    private InterstitialAd mInterstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mInterstitialAd = new InterstitialAd(this.getApplicationContext());
        mInterstitialAd.setAdUnitId("pub-99999999999999999/9999999999");
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice("adb logcat will show you the id")
                .build();
        // Prepare an Interstitial Ad Listener
        mInterstitialAd.setAdListener(new AdListener() {

            @Override
            public void onAdLoaded() {
                displayInterstitial();
            }

            @Override
            public void onAdClosed() {
                startActivity(new Intent(getApplicationContext(),
                        com.dukescript.presenters.Android.class));
                finish();
            }

        });
        mInterstitialAd.loadAd(adRequest);
    }

    public void displayInterstitial() {
        // If Ads are loaded, show Interstitial else show nothing.
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            Toast.makeText(getApplication(), "The asd has not Loaded.", Toast.LENGTH_SHORT).show();
        }

    }

    public static void main(String... args) throws Exception {
        Main.onPageLoad();
    }
}

如果你已经完成了,修改你的pom.xml,不仅包括jar,还包括google play服务的apklib:

    <groupId>com.google.android.gms</groupId>
    <artifactId>google-play-services</artifactId>
    <version>28.0.0</version>
    <type>apklib</type>
</dependency>
<dependency>
    <groupId>com.google.android.gms</groupId>
    <artifactId>google-play-services</artifactId>
    <version>28.0.0</version>
    <type>jar</type>
</dependency>

您还需要修改AndroidManifest.xml并添加一些权限和活动。这是您的应用的代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="web.aprendiendola.dictionaryweb.aprendiendola.dictionary.DiccionarioDeJava"
          android:versionCode="1"
          android:versionName="1.0-SNAPSHOT" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="DiccionarioDeJava"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
        <activity android:name="web.aprendiendola.dictionaryweb.aprendiendola.dictionary.DiccionarioDeJava.AndroidMain" 
                  android:configChanges="orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.dukescript.presenters.Android" 
                  android:configChanges="orientation|screenSize"
                  android:exported="false"
        >
        </activity>
         <activity android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />

        <!-- Configuration section. Defines: 
           - the HTML page to load on start
           - the class that contains the main initialization method
           - name of the initialization method in the given class
        -->
        <meta-data android:name="loadPage" android:value="file:///android_asset/pages/index.html" />
        <meta-data android:name="loadClass" android:value="web.aprendiendola.dictionaryweb.aprendiendola.dictionary.DiccionarioDeJava.AndroidMain" />
        <meta-data android:name="invoke" android:value="main" />
        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />        
    </application>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
</manifest>

就是这样,您现在应该可以展示广告了。它对我发送给你的应用程序很有用。