如何将 admob 集成到 Dukescript 生成的 apk?
How to integrate admob to Dukescript generated apk?
如何将 AdMob 广告功能添加到您的 dukescript 生成的 android 项目中?
例如,我将 android 添加到 Hello World example 并且该应用程序已经发布,因此我可以通过 admob 网站添加一些 ad unit
s,例如广告单元ID ca-app-pub-99999999999999999/9999999999
用于插页式广告和广告单元 ID ca-app-pub-9999999999999999/9999999998
用于横幅。
据我所知,您不能只用 javascript 显示它们,您应该将一些 jar 添加到库文件夹并显式调用它们的一些功能。
您需要添加所需的库。有一个相关的问题如何将它们安装到本地存储库 here。
安装库并在 android 项目中引用它们后,您应该可以使用 AdMob。按照这里的描述去 wrap the DukeScript MainActivity in your own MainActivity. Then you should be able to create ads in the onCreate method 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 播放服务的 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 并添加一些权限和 activity。这是您的应用程序的代码:
<?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>
就是这样,您现在应该可以展示广告了。它适用于您发送给我的应用程序。
如何将 AdMob 广告功能添加到您的 dukescript 生成的 android 项目中?
例如,我将 android 添加到 Hello World example 并且该应用程序已经发布,因此我可以通过 admob 网站添加一些 ad unit
s,例如广告单元ID ca-app-pub-99999999999999999/9999999999
用于插页式广告和广告单元 ID ca-app-pub-9999999999999999/9999999998
用于横幅。
据我所知,您不能只用 javascript 显示它们,您应该将一些 jar 添加到库文件夹并显式调用它们的一些功能。
您需要添加所需的库。有一个相关的问题如何将它们安装到本地存储库 here。
安装库并在 android 项目中引用它们后,您应该可以使用 AdMob。按照这里的描述去 wrap the DukeScript MainActivity in your own MainActivity. Then you should be able to create ads in the onCreate method 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 播放服务的 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 并添加一些权限和 activity。这是您的应用程序的代码:
<?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>
就是这样,您现在应该可以展示广告了。它适用于您发送给我的应用程序。