一、注册腾讯广告账号
注册腾讯广告联盟账号:/dev/
二、新建广告位
新建广告位并下载SDK,记住自己的广告位ID和应用ID。
三、接入SDK
1、解压SDK,将jar文件复制到libs目录中,点击sync按钮(as顶部工具栏)。在中声明权限。
<uses-permission android:name="" />
<uses-permission android:name=".ACCESS_NETWORK_STATE" />
<uses-permission android:name=".ACCESS_WIFI_STATE" />
<uses-permission android:name=".READ_EXTERNAL_STORAGE" />
<uses-permission android:name=".WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name=".READ_PHONE_STATE" />
<uses-permission android:name=".ACCESS_COARSE_LOCATION" />
<uses-permission android:name=".ACCESS_COARSE_UPDATES"/>
2、然后在<application>标签中加入:
<!-- 腾讯声明SDK所需要的组件 -->
<service
android:name=""
android:exported="false" >
</service>
<!-- 请开发者注意字母的大小写,ADActivity,而不是AdActivity -->
<activity
android:name=""
android:configChanges="keyboard|keyboardHidden|orientation|screenSize" >
</activity>
3、新建SplashActivity,布局文件activity_splash.xml,代码如下:
<RelativeLayout
xmlns:andro
android:
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
4、SplashActivity代码如下:
public class SplashActivity extends AppCompatActivity {
private RelativeLayout container;
private boolean canJump;
@SuppressLint("WrongViewCast")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
(savedInstanceState);
setContentView(.activity_splash1);
container = (RelativeLayout) findViewById();
//运行时权限处理
List<String> permissionList = new ArrayList<>();
if ((this,
.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED){
(.READ_PHONE_STATE);
}
if ((this,
.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
(.ACCESS_COARSE_LOCATION);
}
if ((this,
.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
(.WRITE_EXTERNAL_STORAGE);
}
if (!()){
String [] permissions = (new String[()]);
(this,permissions,1);
}else{
requestAds();
}
}
/*
*
* 请求开屏广告
* */
private void requestAds() {
String appId="1106336831";
String adId = "5020929588094641";
new SplashAD(this, container, appId, adId, new SplashADListener() {
@Override
public void onADDismissed() {
//广告显示完毕
forward();
}
@Override
public void onNoAD(AdError adError) {
//广告加载失败
forward();
}
@Override
public void onADPresent() {
//广告加载成功
}
@Override
public void onADClicked() {
//广告被点击
}
@Override
public void onADTick(long l) {
}
});
}
@Override
protected void onPause() {
();
canJump = false;
}
@Override
protected void onResume() {
();
if(canJump) {
forward();
}
canJump = true;
}
private void forward() {
if (canJump){
//跳转到MainActivity
Intent intent = new Intent(this,);
startActivity(intent);
finish();
}else {
canJump = true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 1:
if (>0){
for(int result:grantResults){
if (result != PackageManager.PERMISSION_GRANTED){
(this,"必须同意所有权限才能使用本程序",
Toast.LENGTH_SHORT).show();
finish();
return;
}
}
requestAds();
}else {
(this,"发生未知错误",Toast.LENGTH_SHORT).show();
finish();
}
break;
default:
}
}
}
5、最后将主活动设置为SplashActivity,将AndroidManifest里activity中的.MainActivity修改为.SplashActivity。