说起来有点搞笑,作为一位使用智能机的用户来说,广告这东西很令人讨厌。结果没想到我学习安卓写的第三篇博客竟然就是怎样去写一个简单的广告。
广告和之前第二篇里的进度条有点类似,都是一种耗时操作。
创建一个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、还有一个点系统返回键能返回广告界面,这个我还没去搜索学习。