(新手植入有米廣告教程)根據官方例子給簡單的應用加入有米廣告
阿新 • • 發佈:2019-01-26
第一步,到有米官網中申請一個開發者賬戶並新建一個應用,之後獲取應用的應用祕鑰和釋出ID。如下圖:
第二步,下載有米廣告SDD,並解壓該SDK,把其中libs資料夾中以.arr結尾的檔案複製到AS的新工程libs資料夾中,然後編譯一下。如下圖:選中arr包,然後並點選工具欄紅圈中的東東,這樣arr包就和工程綁定了。
第二點二步:配置應用的build.gradle檔案,記住是應用的,不是工程build.gradle檔案。具體如下圖紅圈所示:加入檔案所沒有的程式碼即可,後編譯一下。
第三步:配置AndroidMainfest.xml檔案
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.weihuan.youmiguanggaotest">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<!-- 以下為可選許可權 -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".BaseActivity"></activity>
<activity
android:name=".SplashActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter >
</activity >
<activity android:name=".MainActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout">
</activity>
<activity
android:name=".SpotAdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout"
android:label="@string/app_name" >
</activity >
<activity
android:name=".SlideableSpotAdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout"
android:label="@string/app_name" >
</activity >
<activity
android:name=".NativeSpotAdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout"
android:label="@string/app_name" >
</activity >
<activity
android:name=".VideoAdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout"
android:label="@string/app_name"
android:resizeableActivity="false"
android:screenOrientation="portrait" >
</activity >
<activity
android:name=".NativeVideoAdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout"
android:label="@string/app_name" >
</activity >
<!-- 視訊元件 -->
<activity
android:name="net.youmi.android.nm.vdo.VideoActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout"
android:resizeableActivity="false"
android:screenOrientation="landscape"
android:taskAffinity=""
android:theme="@android:style/Theme.NoTitleBar" >
</activity >
有米SDK為了相容Android N應用間共享檔案行為變更而需要配置的FileProvider
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.youmi.android.addemo.fileProvider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider" />
</provider >
<!-- 有米渠道號(可選配置) 渠道號不能帶空格,型別為整數 -->
<meta-data
android:name="YOUMI_CHANNEL"
android:value="0" >
</meta-data >
</application>
</manifest>
第四步,新建三個Activity和所屬的XMl佈局檔案,為:SplashActivity、MainActivity、BaseActivity具體程式碼如下:
1、BaseActivity的XML佈局(其實沒用到這佈局)和類中程式碼:
public class BaseActivity extends AppCompatActivity {
protected static final String TAG = "youmi-demo";
protected Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.activity_base);
}
/**
* 列印除錯級別日誌
*
* @param format
* @param args
*/
protected void logDebug(String format, Object... args) {
logMessage(Log.DEBUG, format, args);
}
/**
* 列印資訊級別日誌
*
* @param format
* @param args
*/
protected void logInfo(String format, Object... args) {
logMessage(Log.INFO, format, args);
}
/**
* 列印錯誤級別日誌
*
* @param format
* @param args
*/
protected void logError(String format, Object... args) {
logMessage(Log.ERROR, format, args);
}
/**
* 展示短時Toast
*
* @param format
* @param args
*/
protected void showShortToast(String format, Object... args) {
showToast(Toast.LENGTH_SHORT, format, args);
}
/**
* 展示長時Toast
*
* @param format
* @param args
*/
protected void showLongToast(String format, Object... args) {
showToast(Toast.LENGTH_LONG, format, args);
}
/**
* 列印日誌
*
* @param level
* @param format
* @param args
*/
private void logMessage(int level, String format, Object... args) {
String formattedString = String.format(format, args);
switch (level) {
case Log.DEBUG:
Log.d(TAG, formattedString);
break;
case Log.INFO:
Log.i(TAG, formattedString);
break;
case Log.ERROR:
Log.e(TAG, formattedString);
break;
}
}
/**
* 展示Toast
*
* @param duration
* @param format
* @param args
*/
private void showToast(int duration, String format, Object... args) {
Toast.makeText(mContext, String.format(format, args), duration).show();
}
}
2、SplashActivity的XML佈局檔案和類中程式碼:
public class SplashActivity extends BaseActivity {
private PermissionHelper mPermissionHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
mContext = this;
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 移除標題欄
setContentView(R.layout.activity_splash);
runApp();//直接就運行了
// 當系統為6.0以上時,需要申請許可權
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
mPermissionHelper.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mPermissionHelper.onActivityResult(requestCode, resultCode, data);
}
/**
* 跑應用的邏輯
*/
private void runApp() {
//初始化SDK
AdManager.getInstance(mContext).init("85aa56a59eac8b3d", "a14006f66f58d5d7", true);
//設定開屏
setupSplashAd();
}
/**
* 設定開屏廣告
*/
private void setupSplashAd() {
// 建立開屏容器
final RelativeLayout splashLayout = (RelativeLayout) findViewById(R.id.rl_splash);
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ABOVE, R.id.view_divider);
// 對開屏進行設定
SplashViewSettings splashViewSettings = new SplashViewSettings();
//// 設定是否展示失敗自動跳轉,預設自動跳轉
//splashViewSettings.setAutoJumpToTargetWhenShowFailed(false);
// 設定跳轉的視窗類
splashViewSettings.setTargetClass(MainActivity.class);
// 設定開屏的容器
splashViewSettings.setSplashViewContainer(splashLayout);
// 展示開屏廣告
SpotManager.getInstance(mContext)
.showSplash(mContext, splashViewSettings, new SpotListener() {
@Override
public void onShowSuccess() {
logInfo("開屏展示成功");
}
@Override
public void onShowFailed(int errorCode) {
logError("開屏展示失敗");
switch (errorCode) {
case ErrorCode.NON_NETWORK:
logError("網路異常");
break;
case ErrorCode.NON_AD:
logError("暫無開屏廣告");
break;
case ErrorCode.RESOURCE_NOT_READY:
logError("開屏資源還沒準備好");
break;
case ErrorCode.SHOW_INTERVAL_LIMITED:
logError("開屏展示間隔限制");
break;
case ErrorCode.WIDGET_NOT_IN_VISIBILITY_STATE:
logError("開屏控制元件處在不可見狀態");
break;
default:
logError("errorCode: %d", errorCode);
break;
}
}
@Override
public void onSpotClosed() {
logDebug("開屏被關閉");
}
@Override
public void onSpotClicked(boolean isWebPage) {
logDebug("開屏被點選");
logInfo("是否是網頁廣告?%s", isWebPage ? "是" : "不是");
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
// 開屏展示介面的 onDestroy() 回撥方法中呼叫
SpotManager.getInstance(mContext).onDestroy();
}
}
3、MainActivity的XML佈局和程式碼:
XML佈局
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_activity"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/tv_main_top_tag"
style="@style/style_text"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="@string/tv_main_top_tag"
android:textSize="24sp"
/>
<TextView
android:id="@+id/tv_main_version_info"
style="@style/style_text"
android:layout_below="@id/tv_main_top_tag"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:text="@string/tv_main_version_info"
android:textSize="18sp"
/>
<Button
android:id="@+id/btn_main_show_spot_ad"
style="@style/style_button"
android:layout_below="@id/tv_main_version_info"
android:layout_marginTop="40dp"
android:text="@string/btn_main_show_spot_ad"
/>
<Button
android:id="@+id/btn_main_show_slideable_spot_ad"
style="@style/style_button"
android:layout_below="@id/btn_main_show_spot_ad"
android:layout_marginTop="12dp"
android:text="@string/btn_main_show_slideable_spot_ad"
/>
<Button
android:id="@+id/btn_main_show_native_spot_ad"
style="@style/style_button"
android:layout_below="@id/btn_main_show_slideable_spot_ad"
android:layout_marginTop="12dp"
android:text="@string/btn_main_show_native_spot_ad"
/>
<Button
android:id="@+id/btn_main_show_video_ad"
style="@style/style_button"
android:layout_below="@id/btn_main_show_native_spot_ad"
android:layout_marginTop="12dp"
android:text="@string/btn_main_show_video_ad"
/>
<Button
android:id="@+id/btn_main_show_native_video_ad"
style="@style/style_button"
android:layout_below="@id/btn_main_show_video_ad"
android:layout_marginTop="12dp"
android:text="@string/btn_main_show_native_video_ad"
/>
<Button
android:id="@+id/btn_main_check_ad_settings"
style="@style/style_button"
android:layout_below="@id/btn_main_show_native_video_ad"
android:layout_marginTop="12dp"
android:text="@string/btn_main_check_ad_settings"
/>
<LinearLayout
android:id="@+id/ll_main_banner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
>
</LinearLayout >
<TextView
style="@style/style_text"
android:layout_below="@id/btn_main_check_ad_settings"
android:layout_centerHorizontal="true"
android:layout_marginBottom="12dp"
android:layout_marginTop="24dp"
android:text="@string/tv_main_bottom_tag"
android:textSize="16sp"
/>
</RelativeLayout >
</ScrollView >
MainActivity 程式碼
public class MainActivity extends BaseActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 設定應用版本資訊
setupAppVersionInfo();
// 初始化檢視
initView();
// 預載入資料
preloadData();
// 檢查廣告配置
checkAdSettings();
//設定廣告條
setupBannerAd();
}
/**
* 設定應用版本資訊
*/
private void setupAppVersionInfo() {
TextView textVersionInfo = (TextView) findViewById(R.id.tv_main_version_info);
if (textVersionInfo != null) {
textVersionInfo.append(getAppVersionName());
}
}
/**
* 初始化檢視
*/
private void initView() {
findViewById(R.id.btn_main_show_spot_ad).setOnClickListener(this);
findViewById(R.id.btn_main_show_slideable_spot_ad).setOnClickListener(this);
findViewById(R.id.btn_main_show_native_spot_ad).setOnClickListener(this);
findViewById(R.id.btn_main_show_video_ad).setOnClickListener(this);
findViewById(R.id.btn_main_show_native_video_ad).setOnClickListener(this);
}
/**
* 預載入資料
*/
private void preloadData() {
// 設定伺服器回撥 userId,一定要在請求廣告之前設定,否則無效
VideoAdManager.getInstance(mContext).setUserId("userId");
// 請求視訊廣告
VideoAdManager.getInstance(mContext).requestVideoAd(mContext);
}
/**
* 檢查廣告配置
*/
private void checkAdSettings() {
Button btnCheckAdSettings = (Button) findViewById(R.id.btn_main_check_ad_settings);
btnCheckAdSettings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean result = VideoAdManager.getInstance(mContext).checkVideoAdConfig();
showShortToast("配置 %s", result ? "正確" : "不正確,請對照文件檢查是否存在遺漏");
}
});
}
/**
* 設定廣告條廣告
*/
private void setupBannerAd() {
/**
* 懸浮佈局
*/
// 例項化LayoutParams
FrameLayout.LayoutParams layoutParams =
new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// 設定廣告條的懸浮位置,這裡示例為右下角
layoutParams.gravity = Gravity.BOTTOM | Gravity.RIGHT;
// 獲取廣告條
final View bannerView = BannerManager.getInstance(mContext)
.getBannerView(mContext, new BannerViewListener() {
@Override
public void onRequestSuccess() {
logInfo("請求廣告條成功");
}
@Override
public void onSwitchBanner() {
logDebug("廣告條切換");
}
@Override
public void onRequestFailed() {
logError("請求廣告條失敗");
}
});
// 新增廣告條到視窗中
((Activity) mContext).addContentView(bannerView, layoutParams);
}
@Override
protected void onDestroy() {
super.onDestroy();
// 展示廣告條視窗的 onDestroy() 回撥方法中呼叫
BannerManager.getInstance(mContext).onDestroy();
// 退出應用時呼叫,用於釋放資源
// 如果無法保證應用主介面的 onDestroy() 方法被執行到,請移動以下介面到應用的退出邏輯裡面呼叫
// 插屏廣告(包括普通插屏廣告、輪播插屏廣告、原生插屏廣告)
SpotManager.getInstance(mContext).onAppExit();
// 視訊廣告(包括普通視訊廣告、原生視訊廣告)
VideoAdManager.getInstance(mContext).onAppExit();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
// 插屏廣告
case R.id.btn_main_show_spot_ad:
startActivity(new Intent(mContext, SpotAdActivity.class));
break;
// 輪播插屏廣告
case R.id.btn_main_show_slideable_spot_ad:
startActivity(new Intent(mContext, SlideableSpotAdActivity.class));
break;
// 原生插屏廣告
case R.id.btn_main_show_native_spot_ad:
startActivity(new Intent(mContext, NativeSpotAdActivity.class));
break;
// 視屏廣告
case R.id.btn_main_show_video_ad:
startActivity(new Intent(mContext, VideoAdActivity.class));
break;
// 原生視訊廣告
case R.id.btn_main_show_native_video_ad:
startActivity(new Intent(mContext, NativeVideoAdActivity.class));
break;
default:
break;
}
}
/**
* 獲取應用版本號
*
* @return 應用當前的版本號
*/
private String getAppVersionName() {
try {
PackageManager packageManager = getPackageManager();
return packageManager.getPackageInfo(getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
return null;
}
}
}
執行效果如下:應用開屏廣告、插屏廣告、其它廣告
第二步,下載有米廣告SDD,並解壓該SDK,把其中libs資料夾中以.arr結尾的檔案複製到AS的新工程libs資料夾中,然後編譯一下。如下圖:選中arr包,然後並點選工具欄紅圈中的東東,這樣arr包就和工程綁定了。
第二點二步:配置應用的build.gradle檔案,記住是應用的,不是工程build.gradle檔案。具體如下圖紅圈所示:加入檔案所沒有的程式碼即可,後編譯一下。
第三步:配置AndroidMainfest.xml檔案
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.weihuan.youmiguanggaotest">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<!-- 以下為可選許可權 -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".BaseActivity"></activity>
<activity
android:name=".SplashActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter >
</activity >
<activity android:name=".MainActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout">
</activity>
<activity
android:name=".SpotAdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout"
android:label="@string/app_name" >
</activity >
<activity
android:name=".SlideableSpotAdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout"
android:label="@string/app_name" >
</activity >
<activity
android:name=".NativeSpotAdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout"
android:label="@string/app_name" >
</activity >
<activity
android:name=".VideoAdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout"
android:label="@string/app_name"
android:resizeableActivity="false"
android:screenOrientation="portrait" >
</activity >
<activity
android:name=".NativeVideoAdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout"
android:label="@string/app_name" >
</activity >
<!-- 視訊元件 -->
<activity
android:name="net.youmi.android.nm.vdo.VideoActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout"
android:resizeableActivity="false"
android:screenOrientation="landscape"
android:taskAffinity=""
android:theme="@android:style/Theme.NoTitleBar" >
</activity >
有米SDK為了相容Android N應用間共享檔案行為變更而需要配置的FileProvider
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.youmi.android.addemo.fileProvider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider" />
</provider >
<!-- 有米渠道號(可選配置) 渠道號不能帶空格,型別為整數 -->
<meta-data
android:name="YOUMI_CHANNEL"
android:value="0" >
</meta-data >
</application>
</manifest>
第四步,新建三個Activity和所屬的XMl佈局檔案,為:SplashActivity、MainActivity、BaseActivity具體程式碼如下:
1、BaseActivity的XML佈局(其實沒用到這佈局)和類中程式碼:
public class BaseActivity extends AppCompatActivity {
protected static final String TAG = "youmi-demo";
protected Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.activity_base);
}
/**
* 列印除錯級別日誌
*
* @param format
* @param args
*/
protected void logDebug(String format, Object... args) {
logMessage(Log.DEBUG, format, args);
}
/**
* 列印資訊級別日誌
*
* @param format
* @param args
*/
protected void logInfo(String format, Object... args) {
logMessage(Log.INFO, format, args);
}
/**
* 列印錯誤級別日誌
*
* @param format
* @param args
*/
protected void logError(String format, Object... args) {
logMessage(Log.ERROR, format, args);
}
/**
* 展示短時Toast
*
* @param format
* @param args
*/
protected void showShortToast(String format, Object... args) {
showToast(Toast.LENGTH_SHORT, format, args);
}
/**
* 展示長時Toast
*
* @param format
* @param args
*/
protected void showLongToast(String format, Object... args) {
showToast(Toast.LENGTH_LONG, format, args);
}
/**
* 列印日誌
*
* @param level
* @param format
* @param args
*/
private void logMessage(int level, String format, Object... args) {
String formattedString = String.format(format, args);
switch (level) {
case Log.DEBUG:
Log.d(TAG, formattedString);
break;
case Log.INFO:
Log.i(TAG, formattedString);
break;
case Log.ERROR:
Log.e(TAG, formattedString);
break;
}
}
/**
* 展示Toast
*
* @param duration
* @param format
* @param args
*/
private void showToast(int duration, String format, Object... args) {
Toast.makeText(mContext, String.format(format, args), duration).show();
}
}
2、SplashActivity的XML佈局檔案和類中程式碼:
public class SplashActivity extends BaseActivity {
private PermissionHelper mPermissionHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
mContext = this;
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 移除標題欄
setContentView(R.layout.activity_splash);
runApp();//直接就運行了
// 當系統為6.0以上時,需要申請許可權
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
mPermissionHelper.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mPermissionHelper.onActivityResult(requestCode, resultCode, data);
}
/**
* 跑應用的邏輯
*/
private void runApp() {
//初始化SDK
AdManager.getInstance(mContext).init("85aa56a59eac8b3d", "a14006f66f58d5d7", true);
//設定開屏
setupSplashAd();
}
/**
* 設定開屏廣告
*/
private void setupSplashAd() {
// 建立開屏容器
final RelativeLayout splashLayout = (RelativeLayout) findViewById(R.id.rl_splash);
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ABOVE, R.id.view_divider);
// 對開屏進行設定
SplashViewSettings splashViewSettings = new SplashViewSettings();
//// 設定是否展示失敗自動跳轉,預設自動跳轉
//splashViewSettings.setAutoJumpToTargetWhenShowFailed(false);
// 設定跳轉的視窗類
splashViewSettings.setTargetClass(MainActivity.class);
// 設定開屏的容器
splashViewSettings.setSplashViewContainer(splashLayout);
// 展示開屏廣告
SpotManager.getInstance(mContext)
.showSplash(mContext, splashViewSettings, new SpotListener() {
@Override
public void onShowSuccess() {
logInfo("開屏展示成功");
}
@Override
public void onShowFailed(int errorCode) {
logError("開屏展示失敗");
switch (errorCode) {
case ErrorCode.NON_NETWORK:
logError("網路異常");
break;
case ErrorCode.NON_AD:
logError("暫無開屏廣告");
break;
case ErrorCode.RESOURCE_NOT_READY:
logError("開屏資源還沒準備好");
break;
case ErrorCode.SHOW_INTERVAL_LIMITED:
logError("開屏展示間隔限制");
break;
case ErrorCode.WIDGET_NOT_IN_VISIBILITY_STATE:
logError("開屏控制元件處在不可見狀態");
break;
default:
logError("errorCode: %d", errorCode);
break;
}
}
@Override
public void onSpotClosed() {
logDebug("開屏被關閉");
}
@Override
public void onSpotClicked(boolean isWebPage) {
logDebug("開屏被點選");
logInfo("是否是網頁廣告?%s", isWebPage ? "是" : "不是");
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
// 開屏展示介面的 onDestroy() 回撥方法中呼叫
SpotManager.getInstance(mContext).onDestroy();
}
}
3、MainActivity的XML佈局和程式碼:
XML佈局
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_activity"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/tv_main_top_tag"
style="@style/style_text"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="@string/tv_main_top_tag"
android:textSize="24sp"
/>
<TextView
android:id="@+id/tv_main_version_info"
style="@style/style_text"
android:layout_below="@id/tv_main_top_tag"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:text="@string/tv_main_version_info"
android:textSize="18sp"
/>
<Button
android:id="@+id/btn_main_show_spot_ad"
style="@style/style_button"
android:layout_below="@id/tv_main_version_info"
android:layout_marginTop="40dp"
android:text="@string/btn_main_show_spot_ad"
/>
<Button
android:id="@+id/btn_main_show_slideable_spot_ad"
style="@style/style_button"
android:layout_below="@id/btn_main_show_spot_ad"
android:layout_marginTop="12dp"
android:text="@string/btn_main_show_slideable_spot_ad"
/>
<Button
android:id="@+id/btn_main_show_native_spot_ad"
style="@style/style_button"
android:layout_below="@id/btn_main_show_slideable_spot_ad"
android:layout_marginTop="12dp"
android:text="@string/btn_main_show_native_spot_ad"
/>
<Button
android:id="@+id/btn_main_show_video_ad"
style="@style/style_button"
android:layout_below="@id/btn_main_show_native_spot_ad"
android:layout_marginTop="12dp"
android:text="@string/btn_main_show_video_ad"
/>
<Button
android:id="@+id/btn_main_show_native_video_ad"
style="@style/style_button"
android:layout_below="@id/btn_main_show_video_ad"
android:layout_marginTop="12dp"
android:text="@string/btn_main_show_native_video_ad"
/>
<Button
android:id="@+id/btn_main_check_ad_settings"
style="@style/style_button"
android:layout_below="@id/btn_main_show_native_video_ad"
android:layout_marginTop="12dp"
android:text="@string/btn_main_check_ad_settings"
/>
<LinearLayout
android:id="@+id/ll_main_banner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
>
</LinearLayout >
<TextView
style="@style/style_text"
android:layout_below="@id/btn_main_check_ad_settings"
android:layout_centerHorizontal="true"
android:layout_marginBottom="12dp"
android:layout_marginTop="24dp"
android:text="@string/tv_main_bottom_tag"
android:textSize="16sp"
/>
</RelativeLayout >
</ScrollView >
MainActivity 程式碼
public class MainActivity extends BaseActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 設定應用版本資訊
setupAppVersionInfo();
// 初始化檢視
initView();
// 預載入資料
preloadData();
// 檢查廣告配置
checkAdSettings();
//設定廣告條
setupBannerAd();
}
/**
* 設定應用版本資訊
*/
private void setupAppVersionInfo() {
TextView textVersionInfo = (TextView) findViewById(R.id.tv_main_version_info);
if (textVersionInfo != null) {
textVersionInfo.append(getAppVersionName());
}
}
/**
* 初始化檢視
*/
private void initView() {
findViewById(R.id.btn_main_show_spot_ad).setOnClickListener(this);
findViewById(R.id.btn_main_show_slideable_spot_ad).setOnClickListener(this);
findViewById(R.id.btn_main_show_native_spot_ad).setOnClickListener(this);
findViewById(R.id.btn_main_show_video_ad).setOnClickListener(this);
findViewById(R.id.btn_main_show_native_video_ad).setOnClickListener(this);
}
/**
* 預載入資料
*/
private void preloadData() {
// 設定伺服器回撥 userId,一定要在請求廣告之前設定,否則無效
VideoAdManager.getInstance(mContext).setUserId("userId");
// 請求視訊廣告
VideoAdManager.getInstance(mContext).requestVideoAd(mContext);
}
/**
* 檢查廣告配置
*/
private void checkAdSettings() {
Button btnCheckAdSettings = (Button) findViewById(R.id.btn_main_check_ad_settings);
btnCheckAdSettings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean result = VideoAdManager.getInstance(mContext).checkVideoAdConfig();
showShortToast("配置 %s", result ? "正確" : "不正確,請對照文件檢查是否存在遺漏");
}
});
}
/**
* 設定廣告條廣告
*/
private void setupBannerAd() {
/**
* 懸浮佈局
*/
// 例項化LayoutParams
FrameLayout.LayoutParams layoutParams =
new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// 設定廣告條的懸浮位置,這裡示例為右下角
layoutParams.gravity = Gravity.BOTTOM | Gravity.RIGHT;
// 獲取廣告條
final View bannerView = BannerManager.getInstance(mContext)
.getBannerView(mContext, new BannerViewListener() {
@Override
public void onRequestSuccess() {
logInfo("請求廣告條成功");
}
@Override
public void onSwitchBanner() {
logDebug("廣告條切換");
}
@Override
public void onRequestFailed() {
logError("請求廣告條失敗");
}
});
// 新增廣告條到視窗中
((Activity) mContext).addContentView(bannerView, layoutParams);
}
@Override
protected void onDestroy() {
super.onDestroy();
// 展示廣告條視窗的 onDestroy() 回撥方法中呼叫
BannerManager.getInstance(mContext).onDestroy();
// 退出應用時呼叫,用於釋放資源
// 如果無法保證應用主介面的 onDestroy() 方法被執行到,請移動以下介面到應用的退出邏輯裡面呼叫
// 插屏廣告(包括普通插屏廣告、輪播插屏廣告、原生插屏廣告)
SpotManager.getInstance(mContext).onAppExit();
// 視訊廣告(包括普通視訊廣告、原生視訊廣告)
VideoAdManager.getInstance(mContext).onAppExit();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
// 插屏廣告
case R.id.btn_main_show_spot_ad:
startActivity(new Intent(mContext, SpotAdActivity.class));
break;
// 輪播插屏廣告
case R.id.btn_main_show_slideable_spot_ad:
startActivity(new Intent(mContext, SlideableSpotAdActivity.class));
break;
// 原生插屏廣告
case R.id.btn_main_show_native_spot_ad:
startActivity(new Intent(mContext, NativeSpotAdActivity.class));
break;
// 視屏廣告
case R.id.btn_main_show_video_ad:
startActivity(new Intent(mContext, VideoAdActivity.class));
break;
// 原生視訊廣告
case R.id.btn_main_show_native_video_ad:
startActivity(new Intent(mContext, NativeVideoAdActivity.class));
break;
default:
break;
}
}
/**
* 獲取應用版本號
*
* @return 應用當前的版本號
*/
private String getAppVersionName() {
try {
PackageManager packageManager = getPackageManager();
return packageManager.getPackageInfo(getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
return null;
}
}
}
執行效果如下:應用開屏廣告、插屏廣告、其它廣告
http://pan.baidu.com/s/1i53LCFj
提取碼:f99d