安卓写一个简单的5s(防刷)(开屏)广告。

说起来有点搞笑,作为一位使用智能机的用户来说,广告这东西很令人讨厌。结果没想到我学习安卓写的第三篇博客竟然就是怎样去写一个简单的广告。

广告和之前第二篇里的进度条有点类似,都是一种耗时操作。

创建一个ActicityAds的活动,在用户登录成功后显示5s倒计时和一个跳过按钮,结束后跳转到对应的界面。

运行结果:

 

源代码:

ActivityAds.java


package com.example.studentmgr;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ActivityAds extends Activity {
    private Integer mProgress;
    static TextView textView;
    private Handler mHandler;
    Boolean bo=true;
    Intent intent = new Intent();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ads);
        
        textView=(TextView)findViewById(R.id.text) ;

        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 0x111) {
                    textView.setText(mProgress.toString()+"s");           //更新进度
                } else {
                    if(bo==true) {
                        intent.setClass(ActivityAds.this, ActivityMain.class);
                        startActivity(intent);
                    }

                }
            }
        };

        new Thread(new Runnable() {

                public void run () {
                    mProgress = 6;
                    while (true) {//循环获取耗时操作完成的百分比,直到耗时操作结束
                        try {
                            Thread.sleep(1000);                    //线程休眠1秒
                        } catch (InterruptedException e) {
                            e.printStackTrace();                //输出异常信息
                        }//获取耗时操作完成的百分比
                        Message m = new Message();            //创建并实例化一个消息对象
                        if (mProgress > 1) {        //当完成进度不到100时表示耗时任务未完成
                            m.what = 0x111;                    //设置代表耗时操作未完成的消息代码
                            mHandler.sendMessage(m);        //发送信息
                        } else {                                //当完成进度到达100时表示耗时操作完成
                            m.what = 0x110;                    //设置代表耗时操作已经完成的消息代码
                            mHandler.sendMessage(m);        //发送消息
                            break;                             //退出循环
                        }
                        mProgress--;
                    }
                }
        }).start();
        
        Button button = (Button) findViewById(R.id.skip);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bo=false;
                intent.setClass(ActivityAds.this, ActivityMain.class);
                startActivity(intent);
            }

        });
    }
}

activity_ads.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg1"
    tools:context="com.example.studentmgr.ActivityAds">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_weight="6"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/text"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textSize="15dp"/>
        <Button
            android:id="@+id/skip"
            android:background="@android:color/transparent"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="跳过"
            android:textSize="15dp"/>

    </LinearLayout>
    
</LinearLayout>

问题及总结

1、给TextView设值的时候,一开始使用的int类型,会报错,后改用Integer类型就好了。其中的原因我不太了解,如果有愿意指明的,请大佬在评论区指出。

2、点击跳过跳转到下一个界面后,会再次跳转,只不过是同一个界面。这是因为跳过之后,线程还没有结束。于是我想使用stop()方法,结果安卓Studio显示没有这个方法。然后使用interrupt方法,该方法是能实现,但线程一直是打断状态,不会死亡,所以也不太好。于是我想到了在点击跳过的时候,放一个标签bo值改变为false,然后再线程中,判断,如果标签为true,在最后一秒,正常跳转。如果为false,则什么都不做,但让线程运行完。

3、还有一个点系统返回键能返回广告界面,这个我还没去搜索学习。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值