Android项目集成穿山甲开屏/插屏/横幅广告教程大全
开发及项目环境说明
Android Studio 2020.3.1 Patch 4/203.7717.56.2031.7935034 jdk11.0.9
Android Gradle Plugin Version 7.0.4 Gradle Version 7.0.2
compileSdk 31 minSdk 21 targetSdk 31 穿山甲com.pangle.cn:ads-sdk-pro:4.4.0.9
因为Gradle Version过高,官方的集成文档就很鸡肋老是报错,这里用全新的方式settings.gradle 中集成。
集成初始化SDK和工具类
集成SDK
Gradle Version 7.0.2版本较高的情况下使用这种方式,若比较低请参考官方接入文档。
从穿山甲3.5.0.6版本开始,开发者也可以使用Gradle依赖导入穿山甲SDK。
请勿在project级别的build.gradle文件中添加Maven的引用。要在settings.gradle文件中添加Maven的引用,url ‘https://artifact.bytedance.com/repository/pangle’
示例:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven {
url 'https://artifact.bytedance.com/repository/pangle'
}
}
}
rootProject.name = "项目名"
include ':app'
添加依赖
需要在主module的build.gradle文件添加SDK依赖。
dependencies {
implementation 'com.pangle.cn:ads-sdk-pro:4.4.0.9'
}
AndroidManifest配置
添加权限
<uses-permission android:name="android.permission.INTERNET" />
<permission
android:name="${applicationId}.openadsdk.permission.TT_PANGOLIN"
android:protectionLevel="signature" />
<uses-permission android:name="${applicationId}.openadsdk.permission.TT_PANGOLIN" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
provider配置
注意:
(1)为不影响下载类型广告使用 无论APP处于任何阶段provider都需要在清单文件中正常配置
(2)为不影响到广告的转化及收益 请务必在清单文件中配置xxx.TTMultiProvider
(3)${applicationId} 必须与开发者包名保持一致,否则会引发崩溃问题 适配Anroid7.0及以上如果您的应用需要在Anroid7.0及以上环境运行,请在AndroidManifest 中添加如下代码:
<provider
android:name="com.bytedance.sdk.openadsdk.TTFileProvider"
android:authorities="${applicationId}.TTFileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
在res/xml目录下,新建一个xml文件file_paths,在该文件中添加如下代码:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="tt_external_root" path="." />
<external-path name="tt_external_download" path="Download" />
<external-files-path name="tt_external_files_download" path="Download" />
<files-path name="tt_internal_file_download" path="Download" />
<cache-path name="tt_internal_cache_download" path="Download" />
</paths>
为了适配下载和安装相关功能,在工程中引用的包 com.android.support:support-v4:24.2.0 使用24.2.0以及以上版本
注意:单进程或多进程都必须配置
<provider
android:name="com.bytedance.sdk.openadsdk.multipro.TTMultiProvider"
android:authorities="${applicationId}.TTMultiProvider"
android:exported="false" />
运行环境配置
本项目 minSdkVersion="21" android:targetSdkVersion="31"
代码混淆
如果您需要使用proguard混淆代码,需确保不要混淆SDK的代码。 请在proguard.cfg文件(或其他混淆文件)尾部添加如下配置:
官方说4000版本混淆规则:以aar包里的混淆文件为准我这里使用的是 ads-sdk-pro:4.4.0.9当前最新版 就直接在文件proguard-rules.pro 中添加混淆规则如下:
-keep class com.bytedance.sdk.openadsdk.** { *; }
-keep class com.bytedance.frameworks.** { *; }
-keep class ms.bd.c.Pgl.**{*;}
-keep class com.bytedance.mobsec.metasec.ml.**{*;}
-keep class com.ss.android.**{*;}
-keep class com.bytedance.embedapplog.** {*;}
-keep class com.bytedance.embed_dr.** {*;}
-keep class com.bykv.vk.** {*;}
架构及白名单
注意: 3900以及以上版本SDK默认支持armeabi-v7a ,arm64-v8a 两种架构(我项目中没有配置,直接使用了默认支持) ,如果有其他架构需求,请联系技术支持; 3900以下版本SDK中使用的so文件支持五种架构:x86,x86_64,armeabi,armeabi-v7a,arm64-v8a如果您应用中支持的架构超出这 五种,请在build.gradle中使用abiFilters选择支持的架构。如下所示:
ndk {
abiFilters armeabi-v7a , arm64-v8a , x86 , x86_64 , armeabi
}
注意:平台SDK包中whiteList.txt 白名单上的资源不支持混淆
初始化SDK类
配置完成之后创建一个SDK初始化类TTAdManagerHolder.java ,初始化调用是在开屏界面调用,调用初始化成功之后会自动调用开屏代码,内容如下:
import android.content.Context;
import android.util.Log;
import com.bytedance.sdk.openadsdk.TTAdConfig;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdSdk;
public class TTAdManagerHolder {
private static final String TAG = "TTAdManagerHolder";
private static boolean sInit;
private static String appid = "这里写在穿山甲创建的应用id";
private static String appName = "广告测试";
private static TTAdManagerHolder _Instance;
public static TTAdManager get() {
return TTAdSdk.getAdManager();
}
public static TTAdManagerHolder Inst() {
if (_Instance == null) {
_Instance = new TTAdManagerHolder();
}
return _Instance;
}
public void init(final Context context) {
doInit(context);
}
private void doInit(Context context) {
if (!sInit) {
TTAdSdk.init(context, buildConfig(context), new TTAdSdk.InitCallback() {
@Override
public void success() {
Log.i(TAG, "success: ");
}
@Override
public void fail(int code, String msg) {
Log.i(TAG, "fail: code = " + code + " msg = " + msg);
}
});
sInit = true;
}
}
private TTAdConfig buildConfig(Context context) {
return new TTAdConfig.Builder()
.appId(appid)
.useTextureView(true)
.appName(appName)
.titleBarTheme(TTAdConstant.TITLE_BAR_THEME_DARK)
.allowShowNotify(true)
.debug(true)
.directDownloadNetworkType(TTAdConstant.NETWORK_STATE_WIFI)
.supportMultiProcess(false)
.asyncInit(true)
.build();
}
}
新建类MyApplication.java 用来做初始化,配置全局上下文
import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
public class MyApplication extends Application {
@SuppressLint("StaticFieldLeak")
private static Context mContext;
public void onCreate() {
super.onCreate();
mContext = getApplicationContext();
new TTAdManagerHolder().init(this);
}
public static Context getContext() {
return mContext;
}
}
需要在此AndroidManifest.xml 文件做配置即可:
<application
android:name=".MyApplication" <!-- 添加这行 -->
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:requestLegacyExternalStorage="true"
android:supportsRtl="true"
android:theme="@style/Theme.CsjDemo"
android:usesCleartextTraffic="true"
tools:targetApi="m">
<activity
。。。。。。。
注意: 穿山甲SDK不强制获取AndroidManifest.xml 中权限,即使没有获取可选权限SDK也能正常运行;获取以上权限将帮助穿山甲优化投放广告精准度和用户的交互体验,提高eCPM。
建议在广告请求前,合适的时机调用SDK提供的方法,在用户可以授权的情况下获取到声明中的权限,提高广告变现效率,申请通话、位置、存储权限,代码如下:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TTAdManagerHolder.get().requestPermissionIfNecessary(MyApplication.getContext());
}
}
SDK集成初始化完成之后开始接入SDK。
一些工具类
屏幕适配工具类
UIUtils.java
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.DisplayCutout;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.WindowInsets;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class UIUtils {
public static float getScreenWidthDp(Context context) {
final float scale = context.getResources().getDisplayMetrics().density;
float width = context.getResources().getDisplayMetrics().widthPixels;
return width / (scale <= 0 ? 1 : scale) + 0.5f;
}
public static float getHeight(Activity activity) {
hideBottomUIMenu(activity);
float height;
int realHeight = getRealHeight(activity);
if (UIUtils.hasNotchScreen(activity)) {
height = px2dip(activity, realHeight - getStatusBarHeight(activity));
} else {
height = px2dip(activity, realHeight);
}
return height;
}
public static void hideBottomUIMenu(Activity activity) {
if (activity == null) {
return;
}
try {
if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {
View v = activity.getWindow().getDecorView();
v.setSystemUiVisibility(View.GONE);
} else if (Build.VERSION.SDK_INT >= 19) {
View decorView = activity.getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE;
decorView.setSystemUiVisibility(uiOptions);
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static int getRealHeight(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
display.getRealMetrics(dm);
} else {
display.getMetrics(dm);
}
int realHeight = dm.heightPixels;
return realHeight;
}
public static float getStatusBarHeight(Context context) {
float height = 0;
int resourceId = context.getApplicationContext().getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
height = context.getApplicationContext().getResources().getDimensionPixelSize(resourceId);
}
return height;
}
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / (scale <= 0 ? 1 : scale) + 0.5f);
}
public static int dp2px(Context context, float dp) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}
public static boolean hasNotchScreen(Activity activity) {
return isAndroidPHasNotch(activity)
|| getInt("ro.miui.notch", activity) == 1
|| hasNotchAtHuawei(activity)
|| hasNotchAtOPPO(activity)
|| hasNotchAtVivo(activity);
}
public static boolean isAndroidPHasNotch(Activity activity) {
boolean result = false;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
DisplayCutout displayCutout = null;
try {
WindowInsets windowInsets = activity.getWindow().getDecorView().getRootWindowInsets();
if (windowInsets != null) {
displayCutout = windowInsets.getDisplayCutout();
}
if (displayCutout != null) {
result = true;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public static int getInt(String key, Activity activity) {
int result = 0;
if (isMiui()) {
try {
ClassLoader classLoader = activity.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties = classLoader.loadClass("android.os.SystemProperties");
@SuppressWarnings("rawtypes")
Class[] paramTypes = new Class[2];
paramTypes[0] = String.class;
paramTypes[1] = int.class;
Method getInt = SystemProperties.getMethod("getInt", paramTypes);
Object[] params = new Object[2];
params[0] = new String(key);
params[1] = new Integer(0);
result = (Integer) getInt.invoke(SystemProperties, params);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
return result;
}
public static boolean hasNotchAtHuawei(Context context) {
boolean ret = false;
try {
ClassLoader classLoader = context.getClassLoader();
Class HwNotchSizeUtil = classLoader.loadClass("com.huawei.android.util.HwNotchSizeUtil");
Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
ret = (boolean) get.invoke(HwNotchSizeUtil);
} catch (ClassNotFoundException e) {
} catch (NoSuchMethodException e) {
} catch (Exception e) {
} finally {
return ret;
}
}
public static final int VIVO_NOTCH = 0x00000020;
public static final int VIVO_FILLET = 0x00000008;
public static boolean hasNotchAtVivo(Context context) {
boolean ret = false;
try {
ClassLoader classLoader = context.getClassLoader();
Class FtFeature = classLoader.loadClass("android.util.FtFeature");
Method method = FtFeature.getMethod("isFeatureSupport", int.class);
ret = (boolean) method.invoke(FtFeature, VIVO_NOTCH);
} catch (ClassNotFoundException e) {
} catch (NoSuchMethodException e) {
} catch (Exception e) {
} finally {
return ret;
}
}
public static boolean hasNotchAtOPPO(Context context) {
String temp = "com.kllk.feature.screen.heteromorphism";
String name = getKllkDecryptString(temp);
return context.getPackageManager().hasSystemFeature(name);
}
public static boolean isMiui() {
boolean sIsMiui = false;
try {
Class<?> clz = Class.forName("miui.os.Build");
if (clz != null) {
sIsMiui = true;
return sIsMiui;
}
} catch (Exception e) {
}
return sIsMiui;
}
public static String getKllkDecryptString(String encryptionString) {
if (TextUtils.isEmpty(encryptionString)) {
return "";
}
String decryptTag = "";
String decryptCapitalized = "O" + "P" + "P" + "O";
String decrypt = "o" + "p" + "p" + "o";
if (encryptionString.contains("KLLK")) {
decryptTag = encryptionString.replace("KLLK", decryptCapitalized);
} else if (encryptionString.contains("kllk")) {
decryptTag = encryptionString.replace("kllk", decrypt);
}
return decryptTag;
}
public static void setViewSize(View view, int width, int height) {
if (view.getParent() instanceof FrameLayout) {
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) view.getLayoutParams();
lp.width = width;
lp.height = height;
view.setLayoutParams(lp);
view.requestLayout();
} else if (view.getParent() instanceof RelativeLayout) {
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) view.getLayoutParams();
lp.width = width;
lp.height = height;
view.setLayoutParams(lp);
view.requestLayout();
} else if (view.getParent() instanceof LinearLayout) {
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) view.getLayoutParams();
lp.width = width;
lp.height = height;
view.setLayoutParams(lp);
view.requestLayout();
}
}
public static int getScreenWidthInPx(Context context) {
DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics();
return dm.widthPixels;
}
public static int getScreenHeightInPx(Context context) {
DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics();
return dm.heightPixels;
}
public static int getScreenHeight(Context context) {
return (int) (getScreenHeightInPx(context) + getStatusBarHeight(context));
}
public static void removeFromParent(View view) {
if (view != null) {
ViewParent vp = view.getParent();
if (vp instanceof ViewGroup) {
((ViewGroup) vp).removeView(view);
}
}
}
public static int[] getScreenSize(Context context) {
int[] size = new int[]{0, 0};
if (context == null) {
return size;
}
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
display.getRealMetrics(dm);
} else {
display.getMetrics(dm);
}
size[0] = dm.widthPixels;
size[1] = dm.heightPixels;
return size;
}
}
弹窗提示工具类
TToast.java
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
public final class TToast {
private static Toast sToast;
public static void show(Context context, String msg) {
show(context, msg, Toast.LENGTH_SHORT);
}
public static void show(Context context, String msg, int duration) {
Toast toast = getToast(context);
if (toast != null) {
toast.setDuration(duration);
toast.setText(String.valueOf(msg));
toast.show();
} else {
Log.i("TToast", "toast msg: " + String.valueOf(msg));
}
}
@SuppressLint("ShowToast")
private static Toast getToast(Context context) {
if (context == null) {
return sToast;
}
sToast = Toast.makeText(context.getApplicationContext(), "", Toast.LENGTH_SHORT);
return sToast;
}
public static void reset() {
sToast = null;
}
}
显示序列工具类
ShowSeqUtils.java(激励视频广告使用)
import android.os.Environment;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ShowSeqUtils {
private FileReader fr;
private BufferedReader br;
private FileWriter fw;
private BufferedWriter bw;
public int loadShowSeq(){
int show_seq = 1;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String data = sdf.format(new Date());
try {
String rootPath = Environment.getExternalStorageDirectory().getPath();
File file = new File(rootPath + "/Android/data/com.snssdk.api/cache/adloadSeqTemp.txt");
if (!file.exists()) {
file.createNewFile();
return show_seq;
}
fr = new FileReader(file);
br = new BufferedReader(fr);
String line = "";
while ((line = br.readLine()) != null) {
String[] temp = line.split(",");
if (temp[0].equals(data)){
show_seq = Integer.parseInt(temp[1]);
}
}
return show_seq;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(fr!=null){
fr.close();
}
if(br!=null){
br.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return show_seq;
}
public void writeToFile(int show_seq){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String data = sdf.format(new Date());
String content = data+","+show_seq;
try {
String rootPath = Environment.getExternalStorageDirectory().getPath();
File file = new File(rootPath + "/Android/data/com.snssdk.api/cache/");
if (!file.exists()) {
file.mkdir();
}
String filename = file.getAbsolutePath()+"/adloadSeqTemp.txt";
file = new File(filename);
if(!file.exists()){
file.createNewFile();
}
fw = new FileWriter(file, false);
fw.write(content);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(fw!=null){
fw.flush();
fw.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
穿山甲之开屏广告
首先创建一个安卓Activity界面,文件名为SplashActivity.java 做开屏界面,写入如下内容:
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import androidx.annotation.Nullable;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
import com.bytedance.sdk.openadsdk.TTSplashAd;
import com.yrj.csjdemo.Utils.TToast;
import com.yrj.csjdemo.Utils.UIUtils;
@SuppressLint("CustomSplashScreen")
public class SplashActivity extends Activity {
private static final String TAG = "SplashActivity";
private TTAdNative mTTAdNative;
private FrameLayout mSplashContainer;
private boolean mForceGoMain;
private static final int AD_TIME_OUT = 3000;
private String mCodeId = "这里写开屏广告代码位id";
private boolean mIsExpress = false;
private boolean mIsHalfSize = false;
private static SplashActivity _Instance;
public static SplashActivity Inst() {
return _Instance;
}
@SuppressWarnings("RedundantCast")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_Instance = this;
setContentView(R.layout.activity_splash);
mSplashContainer = (FrameLayout) findViewById(R.id.splash_container);
mTTAdNative = TTAdManagerHolder.get().createAdNative(this);
hideBottomUIMenu();
getExtraInfo();
loadSplashAd();
}
private void getExtraInfo() {
Intent intent = getIntent();
if (intent == null) {
return;
}
String codeId = intent.getStringExtra("splash_rit");
if (!TextUtils.isEmpty(codeId)) {
mCodeId = codeId;
}
mIsExpress = intent.getBooleanExtra("is_express", false);
mIsHalfSize = intent.getBooleanExtra("is_half_size", false);
}
@Override
protected void onResume() {
if (mForceGoMain) {
goToMainActivity();
}
super.onResume();
}
@Override
protected void onStop() {
super.onStop();
mForceGoMain = true;
}
public void loadSplashAd() {
AdSlot adSlot = null;
float splashWidthDp = UIUtils.getScreenWidthDp(this);
int splashWidthPx = UIUtils.getScreenWidthInPx(this);
int screenHeightPx = UIUtils.getScreenHeight(this);
float screenHeightDp = UIUtils.px2dip(this, screenHeightPx);
float splashHeightDp;
int splashHeightPx;
if (mIsHalfSize) {
splashHeightDp = screenHeightDp * 4 / 5.f;
splashHeightPx = (int) (screenHeightPx * 4 / 5.f);
} else {
splashHeightDp = screenHeightDp;
splashHeightPx = screenHeightPx;
}
if (mIsExpress) {
adSlot = new AdSlot.Builder()
.setCodeId(mCodeId)
.setSupportDeepLink(true)
.setImageAcceptedSize(splashWidthPx, splashHeightPx)
.setExpressViewAcceptedSize(splashWidthDp, splashHeightDp)
.build();
} else {
adSlot = new AdSlot.Builder()
.setCodeId(mCodeId)
.setSupportDeepLink(true)
.setImageAcceptedSize(splashWidthPx, splashHeightPx)
.build();
}
mTTAdNative.loadSplashAd(adSlot, new TTAdNative.SplashAdListener() {
@Override
public void onError(int code, String message) {
Log.d(TAG, "loadSplashAd OnError" + message + " code:" + code);
showToast(message);
goToMainActivity();
}
@Override
public void onTimeout() {
Log.d(TAG, "loadSplashAd onTimeout");
showToast("开屏广告加载超时");
goToMainActivity();
}
@Override
public void onSplashAdLoad(com.bytedance.sdk.openadsdk.TTSplashAd ad) {
Log.d(TAG, "loadSplashAd success");
Log.d(TAG, "开屏广告请求成功");
if (ad == null) {
return;
}
View view = ad.getSplashView();
if (view != null && mSplashContainer != null && !SplashActivity.this.isFinishing()) {
mSplashContainer.removeAllViews();
mSplashContainer.addView(view);
} else {
goToMainActivity();
}
ad.setSplashInteractionListener(new TTSplashAd.AdInteractionListener() {
@Override
public void onAdClicked(View view, int type) {
Log.d(TAG, "onAdClicked");
showToast("开屏广告点击");
}
@Override
public void onAdShow(View view, int type) {
Log.d(TAG, "onAdShow");
showToast("开屏广告展示");
}
@Override
public void onAdSkip() {
Log.d(TAG, "onAdSkip");
showToast("开屏广告跳过");
goToMainActivity();
}
@Override
public void onAdTimeOver() {
Log.d(TAG, "onAdTimeOver");
showToast("开屏广告倒计时结束");
goToMainActivity();
}
});
if (ad.getInteractionType() == TTAdConstant.INTERACTION_TYPE_DOWNLOAD) {
ad.setDownloadListener(new TTAppDownloadListener() {
boolean hasShow = false;
@Override
public void onIdle() {
}
@Override
public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
if (!hasShow) {
showToast("下载中...");
hasShow = true;
}
}
@Override
public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
showToast("下载暂停...");
}
@Override
public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
showToast("下载失败...");
}
@Override
public void onDownloadFinished(long totalBytes, String fileName, String appName) {
showToast("下载完成...");
}
@Override
public void onInstalled(String fileName, String appName) {
showToast("安装完成...");
}
});
}
}
}, AD_TIME_OUT);
}
private void goToMainActivity() {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
mSplashContainer.removeAllViews();
this.finish();
}
private void showToast(String msg) {
TToast.show(this, msg);
}
protected void hideBottomUIMenu() {
if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {
View v = this.getWindow().getDecorView();
v.setSystemUiVisibility(View.GONE);
} else if (Build.VERSION.SDK_INT >= 19) {
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
}
}
}
activity_splash.xml 布局文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/splash_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".SplashActivity">
</FrameLayout>
AndroidMainfest.xml 将SplashActivity 界面当做启动界面,移动.MainActivity 下的启动过滤器到.SplashActivity 下配置即可。
<activity
android:name=".MainActivity"
android:exported="true" />
<activity
android:name=".SplashActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
开屏广告配置完毕。运行项目测试广告展示情况即可!出现问题根据控制台和弹窗提示排查解决。
穿山甲之之插屏广告
首先创建一个类InsertScreen.java ,代码如下:
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdLoadType;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
import com.bytedance.sdk.openadsdk.TTFullScreenVideoAd;
import com.yrj.csjdemo.Utils.TToast;
public class InsertScreen {
private final String TAG = "InsertScreen";
@SuppressLint("StaticFieldLeak")
private static InsertScreen _Instance;
public static InsertScreen Inst() {
if (_Instance == null) {
_Instance = new InsertScreen();
}
return _Instance;
}
private TTAdNative mTTAdNative;
private TTFullScreenVideoAd mttFullVideoAd;
private boolean mIsLoaded = false;
private Context mContext;
public void Init(Context context, String codeId, int orientation) {
mContext = context;
TTAdManager ttAdManager = TTAdManagerHolder.get();
mTTAdNative = ttAdManager.createAdNative(mContext);
loadAd(codeId, orientation);
}
private boolean mHasShowDownloadActive = false;
private void loadAd(String codeId, int orientation) {
AdSlot adSlot = new AdSlot.Builder()
.setCodeId(codeId)
.setSupportDeepLink(true)
.setAdLoadType(TTAdLoadType.LOAD)
.setOrientation(orientation)
.build();
mTTAdNative.loadFullScreenVideoAd(adSlot, new TTAdNative.FullScreenVideoAdListener() {
@Override
public void onError(int code, String message) {
Log.e(TAG, "Callback --> onError: " + code + ", " + String.valueOf(message));
TToast.show(mContext, message);
}
@Override
public void onFullScreenVideoAdLoad(TTFullScreenVideoAd ad) {
Log.e(TAG, "Callback --> onFullScreenVideoAdLoad");
TToast.show(mContext, "FullVideoAd loaded 广告类型:" + getAdType(ad.getFullVideoAdType()));
mttFullVideoAd = ad;
mIsLoaded = false;
mttFullVideoAd.setFullScreenVideoAdInteractionListener(new TTFullScreenVideoAd.FullScreenVideoAdInteractionListener() {
@Override
public void onAdShow() {
Log.d(TAG, "Callback --> FullVideoAd show");
TToast.show(mContext, "FullVideoAd show");
}
@Override
public void onAdVideoBarClick() {
Log.d(TAG, "Callback --> FullVideoAd bar click");
TToast.show(mContext, "FullVideoAd bar click");
}
@Override
public void onAdClose() {
Log.d(TAG, "Callback --> FullVideoAd close");
TToast.show(mContext, "FullVideoAd close");
}
@Override
public void onVideoComplete() {
Log.d(TAG, "Callback --> FullVideoAd complete");
TToast.show(mContext, "FullVideoAd complete");
}
@Override
public void onSkippedVideo() {
Log.d(TAG, "Callback --> FullVideoAd skipped");
TToast.show(mContext, "FullVideoAd skipped");
}
});
ad.setDownloadListener(new TTAppDownloadListener() {
@Override
public void onIdle() {
mHasShowDownloadActive = false;
}
@Override
public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
Log.d("DML", "onDownloadActive==totalBytes=" + totalBytes + ",currBytes=" + currBytes + ",fileName=" + fileName + ",appName=" + appName);
if (!mHasShowDownloadActive) {
mHasShowDownloadActive = true;
TToast.show(mContext, "下载中,点击下载区域暂停", Toast.LENGTH_LONG);
}
}
@Override
public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
Log.d("DML", "onDownloadPaused===totalBytes=" + totalBytes + ",currBytes=" + currBytes + ",fileName=" + fileName + ",appName=" + appName);
TToast.show(mContext, "下载暂停,点击下载区域继续", Toast.LENGTH_LONG);
}
@Override
public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
Log.d("DML", "onDownloadFailed==totalBytes=" + totalBytes + ",currBytes=" + currBytes + ",fileName=" + fileName + ",appName=" + appName);
TToast.show(mContext, "下载失败,点击下载区域重新下载", Toast.LENGTH_LONG);
}
@Override
public void onDownloadFinished(long totalBytes, String fileName, String appName) {
Log.d("DML", "onDownloadFinished==totalBytes=" + totalBytes + ",fileName=" + fileName + ",appName=" + appName);
TToast.show(mContext, "下载完成,点击下载区域重新下载", Toast.LENGTH_LONG);
}
@Override
public void onInstalled(String fileName, String appName) {
Log.d("DML", "onInstalled==" + ",fileName=" + fileName + ",appName=" + appName);
TToast.show(mContext, "安装完成,点击下载区域打开", Toast.LENGTH_LONG);
}
});
}
@Override
public void onFullScreenVideoCached() {
Log.d(TAG, " onFullScreenVideoCached");
TToast.show(mContext, "onFullScreenVideoCached");
}
@Override
public void onFullScreenVideoCached(TTFullScreenVideoAd ad) {
Log.e(TAG, "Callback --> onFullScreenVideoCached");
mIsLoaded = true;
TToast.show(mContext, "FullVideoAd video cached");
ad.showFullScreenVideoAd((Activity) mContext, TTAdConstant.RitScenes.GAME_GIFT_BONUS, null);
}
});
}
private String getAdType(int type) {
switch (type) {
case TTAdConstant.AD_TYPE_COMMON_VIDEO:
return "普通全屏视频,type=" + type;
case TTAdConstant.AD_TYPE_PLAYABLE_VIDEO:
return "Playable全屏视频,type=" + type;
case TTAdConstant.AD_TYPE_PLAYABLE:
return "纯Playable,type=" + type;
case TTAdConstant.AD_TYPE_LIVE:
return "直播流,type=" + type;
}
return "未知类型+type=" + type;
}
}
然后在主界面MainActivity.java 或其他界面的Oncreate()方法下做调用,代码如下:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;
import com.bytedance.sdk.openadsdk.TTAdConstant;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TTAdManagerHolder.get().requestPermissionIfNecessary(MyApplication.getContext());
InsertScreen.Inst().Init(this, "这里写插屏广告代码位id", TTAdConstant.HORIZONTAL);
}
}
插屏广告配置完毕。运行项目测试广告展示情况即可!
穿山甲之Banner广告
首先创建一个类Banner.java ,代码如下:
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdDislike;
import com.bytedance.sdk.openadsdk.TTAdLoadType;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.yrj.csjdemo.Utils.TToast;
import com.yrj.csjdemo.Utils.UIUtils;
import java.util.List;
public class Banner {
private static Banner _Insstance;
public static Banner Inst() {
if (_Insstance == null) {
_Insstance = new Banner();
}
return _Insstance;
}
private TTAdNative mTTAdNative;
private FrameLayout mExpressContainer;
private Context mContext;
private final long startTime = 0;
private boolean mHasShowDownloadActive = false;
public void Init(Context context, FrameLayout frameLayout, String codeId) {
mContext = context;
mExpressContainer = frameLayout;
mTTAdNative = TTAdManagerHolder.get().createAdNative(context);
loadBannerAd(codeId);
}
private void loadBannerAd(String codeId) {
int width = UIUtils.getScreenWidthInPx(mContext);
int height = UIUtils.getScreenHeight(mContext);
AdSlot adSlot = new AdSlot.Builder()
.setCodeId(codeId)
.setSupportDeepLink(true)
.setAdCount(1)
.setExpressViewAcceptedSize(width, 50)
.setAdLoadType(TTAdLoadType.LOAD)
.build();
mTTAdNative.loadBannerExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
@Override
public void onError(int code, String message) {
TToast.show(mContext, "load error : " + code + ", " + message);
mExpressContainer.removeAllViews();
}
@Override
public void onNativeExpressAdLoad(List<TTNativeExpressAd> ads) {
if (ads.get(0) == null) {
return;
}
final TTNativeExpressAd ad = ads.get(0);
ad.setSlideIntervalTime(30 * 1000);
bindAdListener(ad);
ad.render();
TToast.show(mContext, "load success!");
}
});
}
private void bindAdListener(TTNativeExpressAd ad) {
ad.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
@Override
public void onAdClicked(View view, int type) {
TToast.show(mContext, "广告被点击");
}
@Override
public void onAdShow(View view, int type) {
TToast.show(mContext, "广告展示");
}
@Override
public void onRenderFail(View view, String msg, int code) {
Log.e("ExpressView", "render fail:" + (System.currentTimeMillis() - startTime));
TToast.show(mContext, msg + " code:" + code);
}
@Override
public void onRenderSuccess(View view, float width, float height) {
Log.e("ExpressView", "render suc:" + (System.currentTimeMillis() - startTime));
TToast.show(mContext, "渲染成功");
mExpressContainer.removeAllViews();
mExpressContainer.addView(view);
}
});
bindDislike(ad, false);
if (ad.getInteractionType() != TTAdConstant.INTERACTION_TYPE_DOWNLOAD) {
return;
}
ad.setDownloadListener(new TTAppDownloadListener() {
@Override
public void onIdle() {
TToast.show(mContext, "点击开始下载", Toast.LENGTH_LONG);
}
@Override
public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
if (!mHasShowDownloadActive) {
mHasShowDownloadActive = true;
TToast.show(mContext, "下载中,点击暂停", Toast.LENGTH_LONG);
}
}
@Override
public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
TToast.show(mContext, "下载暂停,点击继续", Toast.LENGTH_LONG);
}
@Override
public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
TToast.show(mContext, "下载失败,点击重新下载", Toast.LENGTH_LONG);
}
@Override
public void onInstalled(String fileName, String appName) {
TToast.show(mContext, "安装完成,点击图片打开", Toast.LENGTH_LONG);
}
@Override
public void onDownloadFinished(long totalBytes, String fileName, String appName) {
TToast.show(mContext, "点击安装", Toast.LENGTH_LONG);
}
});
}
private void bindDislike(TTNativeExpressAd ad, boolean customStyle) {
ad.setDislikeCallback((Activity) mContext, new TTAdDislike.DislikeInteractionCallback() {
@Override
public void onShow() {
TToast.show(mContext, "bindDislike setDislikeCallback onShow");
}
@Override
public void onSelected(int position, String value, boolean enforce) {
TToast.show(mContext, "点击 " + value);
mExpressContainer.removeAllViews();
if (enforce) {
TToast.show(mContext, "模版Banner 穿山甲sdk强制将view关闭了");
}
}
@Override
public void onCancel() {
TToast.show(mContext, "点击取消 ");
}
});
}
}
然后在主界面MainActivity.java 或其他界面的布局xml文件中的合适位置,设置广告显示布局,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="MissingConstraints">
<FrameLayout
android:id="@+id/banner_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:ignore="MissingConstraints" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="这里是一行示例布局展示文字"
android:textSize="18sp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
然后在主界面MainActivity.java 或其他界面的Oncreate()方法下做调用,代码如下:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;
import com.bytedance.sdk.openadsdk.TTAdConstant;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TTAdManagerHolder.get().requestPermissionIfNecessary(MyApplication.getContext());
InsertScreen.Inst().Init(this, "这里写插屏广告代码位id", TTAdConstant.HORIZONTAL);
FrameLayout mExpressBannerContainer = findViewById(R.id.banner_container);
Banner.Inst().Init(this, mExpressBannerContainer, "这里写banner广告代码位id");
}
}
Banner广告配置完毕。运行项目测试广告展示情况即可!
穿山甲之信息流广告
暂时不写
穿山甲之激励视频广告
暂时不写
参考链接: 参考作者主页 Android 接入穿山甲广告 Android 接入穿山甲SDK之开屏广告 Android 接入穿山甲SDK之插屏广告 Android 接入穿山甲SDK之信息流广告 Android 接入穿山甲SDK之Banner广告 Android 接入穿山甲SDK之激励视频广告 等等,还有一些其它教程和解错文章,感谢!!
|