一、注册腾讯广告账号
注册腾讯广告联盟账号: http://e.qq.com/dev/index.html
二、新建广告位
新建广告位并下载SDK,记住自己的广告位ID和应用ID。
三、接入SDK
1、解压SDK,将jar文件复制到libs目录中,点击sync按钮(as顶部工具栏)。在AndroidManifest.xml中声明权限。
代码语言:javascript复制 <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES"/>
2、然后在标签中加入:
代码语言:javascript复制<!-- 腾讯声明SDK所需要的组件 -->
<service
android:name="com.qq.e.comm.DownloadService"
android:exported="false" >
</service>
<!-- 请开发者注意字母的大小写,ADActivity,而不是AdActivity -->
<activity
android:name="com.qq.e.ads.ADActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize" >
</activity>
3、新建SplashActivity.java,布局文件activity_splash.xml,代码如下:
代码语言:javascript复制<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@ id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
4、SplashActivity.java 代码如下:
代码语言:javascript复制public class SplashActivity extends AppCompatActivity {
private RelativeLayout container;
private boolean canJump;
@SuppressLint("WrongViewCast")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash1);
container = (RelativeLayout) findViewById(R.id.container);
//运行时权限处理
List<String> permissionList = new ArrayList<>();
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED){
permissionList.add(Manifest.permission.READ_PHONE_STATE);
}
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
permissionList.add(Manifest.permission.ACCESS_COARSE_LOCATION);
}
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (!permissionList.isEmpty()){
String [] permissions = permissionList.toArray(new String[permissionList.size()]);
ActivityCompat.requestPermissions(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() {
super.onPause();
canJump = false;
}
@Override
protected void onResume() {
super.onResume();
if(canJump) {
forward();
}
canJump = true;
}
private void forward() {
if (canJump){
//跳转到MainActivity
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
finish();
}else {
canJump = true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 1:
if (grantResults.length>0){
for(int result:grantResults){
if (result != PackageManager.PERMISSION_GRANTED){
Toast.makeText(this,"必须同意所有权限才能使用本程序",
Toast.LENGTH_SHORT).show();
finish();
return;
}
}
requestAds();
}else {
Toast.makeText(this,"发生未知错误",Toast.LENGTH_SHORT).show();
finish();
}
break;
default:
}
}
}
5、最后将主活动设置为SplashActivity
将AndroidManifest里activity中的.MainActivity修改为.SplashActivity。