Android实现开屏广告(广点通SDK)

一、注册腾讯广告账号

注册腾讯广告联盟账号:http://e.qq.com/dev/index.html

二、新建广告位

新建广告位并下载SDK,记住自己的广告位ID和应用ID。

三、接入SDK

1、解压SDK,将jar文件复制到libs目录中,点击sync按钮(as顶部工具栏)。在AndroidManifest.xml中声明权限。

    
    
    
    
    
    
    
    

2、然后在标签中加入:







3、新建SplashActivity,布局文件activity_splash.xml,代码如下:




4、SplashActivity代码如下:

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 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。

 

 

你可能感兴趣的:(开屏广告,android)