一、splash界面的作用
用来展现产品的logo
应用程序初始化的操作
检查应用程序的版本
检查当前应用程序是否合法注册
二、界面的xml定义
写一个布局背景设置为产品的logo图片,再添加一个textview显示版本号。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<textview android:id= "@+id/tv_splash_version" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:textcolor= "#000000" android:textstyle= "bold" android:shadowdx= "1" //阴影的偏移量 android:shadowdy= "1" android:shadowradius= "0.2" //阴影的半径 android:shadowcolor= "#ffff00" android:text= "版本:1.0" android:textsize= "16sp" android:layout_centerinparent= "true" /> |
三、动态获取版本号的方法
1
2
3
4
5
6
7
8
9
10
11
|
public string getappversion(){ packagemanager pm = getpackagemanager(); try { packageinfo info = pm.getpackageinfo(getpackagename(), 0 ); return info.versionname; } catch (namenotfoundexception e) { e.printstacktrace(); //不可能发生; return "" ; } } |
四、链接服务器获取更新信息
升级提醒的对话框
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
protected void showupdatedialog() { alertdialog.builder build = new builder( this ); build.settitle( "发现新版本" ); build.setmessage(description); build.setnegativebutton( "立刻升级" , new onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { //升级的代码; }; }); build.setpositivebutton( "下次再说" , new onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.dismiss(); enterhome(); } }); build.show(); |
在子线程中请求服务器的代码 checkup()方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
private void checkup() { new thread() { public void run() { message msg = message.obtain(); long starttime = system.currenttimemillis(); //启动该线程的系统时间 try { //请求网络的代码 url url = new url(getstring(r.string.serverurl)); httpurlconnection conn = (httpurlconnection) url .openconnection(); conn.setrequestmethod( "get" ); //请求方法 conn.setconnecttimeout( 4000 ); //超时时间 int code = conn.getresponsecode(); //返回码200请求成功 if (code == 200 ) { inputstream is = conn.getinputstream(); string result = streamtools.readfromstream(is); log.i(tag, "联网成功" + result); jsonobject obj = new jsonobject(result); //解析json字符串 string version = (string) obj.get( "version" ); //版本信息 description = (string) obj.get( "description" ); //描述信息 apkurl = (string) obj.get( "apkurl" ); if (getappversion().equals(version)) { msg.what = enter_home; } else { msg.what = show_update_dialog; } } } catch (malformedurlexception e) { e.printstacktrace(); msg.what = url_error; } catch (ioexception e) { e.printstacktrace(); msg.what = network_error; } catch (jsonexception e) { e.printstacktrace(); msg.what = json_error; } finally { handler.sendmessage(msg); long endtime = system.currenttimemillis(); //该线程执行完毕的时间 long dtime = endtime-starttime; //该线程的阻塞时间 if (dtime< 3000 ) { try { thread.sleep( 3000 -dtime); //若该线程的阻塞时间小于三秒继续睡眠到三秒 } catch (interruptedexception e) { e.printstacktrace(); } } } } }.start(); } handler private handler handler = new handler() { @override public void handlemessage(message msg) { // todo auto-generated method stub super .handlemessage(msg); switch (msg.what) { case show_update_dialog: showupdatedialog(); break ; case enter_home: toast.maketext(getapplicationcontext(), "" , 0 ).show(); enterhome(); break ; case url_error: toast.maketext(getapplicationcontext(), "url_error" , 0 ).show(); enterhome(); break ; case network_error: toast.maketext(getapplicationcontext(), "network_error" , 0 ).show(); enterhome(); break ; case json_error: toast.maketext(getapplicationcontext(), "json_error" , 0 ).show(); enterhome(); break ; } } }; |
五、下载文件(使用afinal框架)并调用系统安装工具安装apk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
if (environment.getexternalstoragestate().equals(environment.media_mounted)) { finalhttp finalhttp = new finalhttp(); finalhttp.download(apkurl,environment.getexternalstoragedirectory().getabsolutepath()+ "/mobilesafe2.0.apk" , new ajaxcallback<file>() { @override public void onloading( long count, long current) { super .onloading(count, current); tv_uapdate_info.setvisibility(view.visible); int progress =( int ) (current* 100 /count); tv_uapdate_info.settext( "下载进度:" +progress+ "%" ); } @override public void onfailure(throwable t, int errorno, string strmsg) { t.printstacktrace(); toast.maketext(getapplicationcontext(), "下载失败" , 0 ).show(); enterhome(); super .onfailure(t, errorno, strmsg); } @override public void onsuccess(file t) { super .onsuccess(t); installapk(t); } private void installapk(file t) { intent intent = new intent(); //自动安装程序可调用该段代码 intent.setaction( "android.intent.action.view" ); intent.addcategory( "android.intent.category.default" ); intent.setdataandtype(uri.fromfile(t), "application/vnd.android.package-archive" ); startactivity(intent); } }); } else { toast.maketext(getapplicationcontext(), "请插入内存卡再试" , 0 ).show(); return ; } |
其他:
1、显示4.0的样式:方式是去掉功能清单里的activity对应的android:theme;
放到application里面;
2、当splash页面弹出升级提示框过滤点击返回的是两种方式:
builder.setcancelable(false);
设置setoncancellistener 当触屏的时候直接进入主页面
对话框是挂载在activity上面的,如果activity不存在,对话框就不能被创建。
getapplicationcontext();生命周期长,只要应用还存活它就存在;this 生命周期短,只要activity不存在了,系统就会回收
其中:getbasecontext(),getapplication(),getapplicationcontext(); 都不能放在alertdialog做上下文;
3.splash用来宣传和隐藏程序启动细节是很有用的。
用handler的实现方法如下:(也可以用线程实现,不推荐)
定义一个activity,用来显示你的图片,其中最重要的就是定义一个handler,用来发送和接收消息:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public class welcomeactivity extends activity { //定义一个handler,用来接收延迟发送的信息-启动activity private handler handler = new handler() { @override <span style= "color: #ff0000;" > public void handlemessage(message msg) </span> { // todo auto-generated method stub super .handlemessage(msg); switch (msg.what) { case 0x123 : intent intent = new intent(welcomeactivity. this , onlineexamactivity. class ); startactivity(intent); finish(); } } }; |
在oncreate()方法中,用handler发送消息,延迟3000毫秒:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super .oncreate(savedinstancestate); setcontentview(r.layout.welcome_activity); start(); } private void start() { <span style= "color: #ff0000;" >handler.sendemptymessagedelayed( 0x123 , 3000 );</span> } |
把你的图片放到布局文件中作背景即可。