首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将admob集成到Dukescript生成的apk中?

如何将admob集成到Dukescript生成的apk中?
EN

Stack Overflow用户
提问于 2015-11-19 15:59:05
回答 1查看 265关注 0票数 0

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

比方说,我将android添加到Hello示例中,并且该应用程序已经发布,这样我就可以通过admob站点添加一些ad unit的单元ID ca-app-pub-99999999999999999/9999999999,例如广告单元ID ca-app-pub-99999999999999999/9999999999作为中间部分,Ad unit ID ca-app-pub-9999999999999999/9999999998表示横幅。

据我所知,您不能仅用javascript显示它们,您应该向库文件夹中添加一些jars,并显式地调用它们的一些函数。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-22 16:06:47

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

在安装好库并在android项目中引用它们之后,您应该能够使用AdMob了。按照这里的描述,在您自己的包装DukeScript MainActivity中使用MainActivity。然后,您应该能够在onCreate方法如前所述中创建广告。

代码语言:javascript
复制
    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服务的apklib:

代码语言:javascript
复制
    <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并添加一些权限和一个活动。下面是您的应用程序的代码:

代码语言:javascript
复制
<?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>

就这样,你现在应该可以显示一个广告了。你寄给我的申请表对我有用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33808674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档