第一步,到有米官网中申请一个开发者账户,然后在当前页面中添加一个应用,之后获取应用的应用秘钥和发布ID。如下图:
第二步,下载有米广告SDD,并解压该SDK,把其中libs文件夹中以.arr结尾的文件复制到AS的新工程libs文件夹中,然后编译一下。
然后配置应用的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;
}
}
}
运行效果如下:应用开屏广告、插屏广告、其它广告
Demo下载链接:有米广告demo
2024最新激活全家桶教程,稳定运行到2099年,请移步至置顶文章:https://sigusoft.com/99576.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。 文章由激活谷谷主-小谷整理,转载请注明出处:https://sigusoft.com/151689.html