Android之Splash页面
在继上个任务没有解决之后,心灰意冷之后,现在的我在跟着视频学习开发一个手机卫士的软件。在写自己的笔记之前,我先来展示一下我的结果。


下面我来总结一下我跟随视频学习到的知识点:
一、代码的组织结构:
1.按业务逻辑划分
银行管理系统
com.icbc.money 薪资管理
com.icbc.sms 短信
com.icbc.travel 员工出差
网盘:
com.sina.vdisk.upload 上传
com.sina.vdisk.upload 上传
com.sina.vdisk.share 分享
2.按功能模块划分(推荐用此方法)
com.itheima.mobilesafe58.activity 安放activity
com.itheima.mobilesafe58.service 系统服务
com.itheima.mobilesafe58.receive 广播
com.itheima.mobilesafe58.utils 工具封装
上面的图片展示我学习建立工程的第一步就是当你打开一个app的时候,出现的以一个界面。那么这个界面的作用是什么呢?
1、展示品牌
2、 初始化数据
3、检查版本
4、校验合法性,是否必须联网
首先建立布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash" //插入的背景图片 首先需要把图片复制到res/drawble-hdpi下面,然后就可以在此代码中实现
tools:context=".SplashActivity" > <TextView
android:id="@+id/tv_version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:shadowColor="#f00"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="1"
android:textSize="16sp"
android:text="版本名为:1.0" /> <ProgressBar
android:id="@+id/pb_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/tv_version"
android:layout_centerHorizontal="true"
android:layout_marginBottom="170dp"/> <TextView
android:id="@+id/tv_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:textColor="#f00"/> </RelativeLayout>
布局文件的知识点: 怎么避免插入的图片的失真? 首先需要把图片复制到res/drawble-hdpi下面,然后就可以在android:background="@drawable/splash"代码中实现添加背景的效果。
下面的属性给版本号添加了阴影的效果:
android:shadowColor="#f00" //指定阴影的颜色
android:shadowDx="1" //指定阴影在X轴、Y轴的偏移量以及阴影的半径
android:shadowDy="1"
android:shadowRadius="1" //阴影的半径必须设置,当数值为0时,无阴影。数值越大会越透明,扩散效果越明显
二、搭建服务器
视频当中是以Tomcat作为服务器。按照教程顺利搭建了服务器
下面是搭建服务器成功的时候看到的网页图片:![]()
然后写一个versionCode.json文件,当有新的功能高的软件被开发出来用以替换之前的软件。
代码如下:
{
"versionName":"3.0",
"versionCode":3,
"description":"发现新版本,快开下载!!!",
"downloadUrl":"http://www.baidu.com"
}
然后对这个文件进行测试一下,结果显示为:

下面是我的Activity代码:
package com.itcast.mobilesafe58.activity; import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; import org.json.JSONException;
import org.json.JSONObject; import com.itcast.mobilesafe58.utils.StreamUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack; import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast; public class SplashActivity extends Activity {
private TextView tvVersion;
private TextView tvProgress;
private String mVersionName;//成员变量
private int mVersionCode;
private String mDescription;
private String mDownloadUrl; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
tvVersion = (TextView)findViewById(R.id.tv_version);
tvProgress = (TextView)findViewById(R.id.tv_progress);
tvVersion.setText("版本名:"+getVersionName());
checkVersion();
}
/*
* 检查版本更新
*/
private void checkVersion() {
// TODO Auto-generated method stub
new Thread(){ public void run(){
try {
//
URL url = new URL("http://10.0.2.2:8080/versionCode.json");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(3000);//连接超时
conn.setReadTimeout(3000);//读取超时
conn.setRequestMethod("GET");//请求方法
conn.connect();
int responseCode = conn.getResponseCode();//获取相应
if(responseCode == 200){
String result = StreamUtils.streamToString(conn.getInputStream());
System.out.println("访问成功--->"+result);//打印日志文件 JSONObject jo = new JSONObject(result);
mVersionName = jo.getString("versionName");
mVersionCode = jo.getInt("versionCode");
mDescription = jo.getString("description");
mDownloadUrl = jo.getString("downloadUrl");
System.out.println("versionCode--->"+mVersionCode);
if (getVersionCode()<mVersionCode) {
System.out.println("有新版本!!!");
}else{
System.out.println("没有新版本!!!");
}
}
} catch (MalformedURLException e) {
//url异常
// TODO Auto-generated catch block
System.out.println("url异常!!!");
e.printStackTrace();
} catch (IOException e) {
//
// TODO Auto-generated catch block
System.out.println("连接异常!!!");
e.printStackTrace();
}catch (JSONException e) {
//json异常
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("json解析异常");
}
}
}.start(); } /*
* 获取版本号
*/
private int getVersionCode() {
// TODO Auto-generated method stub
PackageManager pm = getPackageManager();
try {
PackageInfo packageInfo = pm.getPackageInfo(getPackageName(), 0);
int versionCode = packageInfo.versionCode;
return versionCode;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
} }
在连接服务器时,遇到的问题不是太大,但是解析json的时候遇到的问题有:
1、解析异常
检查到的错误是:json书写的格式有问题。应该是直接是{},但是之前写的是json{}。还有最后一个key,value不用加,号。
编码格式不一致。Android默认是GBK,但是json是UTF-8的格式。
当问题解决后运行的结果为:

下面我来讲解一下第二个界面
package com.itcast.mobilesafe58.activity; import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; import org.json.JSONException;
import org.json.JSONObject; import com.itcast.mobilesafe58.utils.StreamUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack; import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast; public class SplashActivity extends Activity {
private static final int UPDATE_DIALOG = 1;
private TextView tvVersion;
private TextView tvProgress;
//服务器的返回值
private String mVersionName;//成员变量
private int mVersionCode;
private String mDescription;
private String mDownloadUrl; private Handler mHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case UPDATE_DIALOG:
showUpdateDialog();
break; default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
tvVersion = (TextView)findViewById(R.id.tv_version);
tvProgress = (TextView)findViewById(R.id.tv_progress);
tvVersion.setText("版本名:"+getVersionName());
checkVersion();
}
/*
* 检查版本更新
*/
private void checkVersion() {
// TODO Auto-generated method stub
new Thread(){
Message msg = Message.obtain();
public void run(){
try {
//
URL url = new URL("http://10.0.2.2:8080/versionCode.json");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(3000);//
conn.setReadTimeout(3000);//
conn.setRequestMethod("GET");//
conn.connect();
int responseCode = conn.getResponseCode();
if(responseCode == 200){
String result = StreamUtils.streamToString(conn.getInputStream());
System.out.println("访问成功--->"+result); JSONObject jo = new JSONObject(result);
mVersionName = jo.getString("versionName");
mVersionCode = jo.getInt("versionCode");
mDescription = jo.getString("description");
mDownloadUrl = jo.getString("downloadUrl");
System.out.println("versionCode--->"+mVersionCode);
if (getVersionCode()<mVersionCode) {
System.out.println("有新版本!!!");
msg.what = UPDATE_DIALOG;
}else{
System.out.println("没有新版本!!!");
}
}
} catch (MalformedURLException e) {
//url异常
// TODO Auto-generated catch block
System.out.println("url异常!!!");
e.printStackTrace();
} catch (IOException e) {
//
// TODO Auto-generated catch block
System.out.println("连接异常!!!");
e.printStackTrace();
}catch (JSONException e) {
//json异常
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("json解析异常");
}finally{
mHandler.sendMessage(msg);
}
} }.start(); }
protected void showUpdateDialog() {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("发现新版本:"+mVersionName);
builder.setMessage(mDescription);
builder.setPositiveButton("立即升级", new OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
System.out.println("发现新版本");
}
});
builder.setNegativeButton("以后再说", null);
builder.show(); } private String getVersionName() {
// TODO Auto-generated method stub
PackageManager pm = getPackageManager();
try {
PackageInfo packageInfo = pm.getPackageInfo(getPackageName(), 0);
int versionCode = packageInfo.versionCode;
String versionName = packageInfo.versionName;
System.out.println("versionName"+versionName+",versionCode"+versionCode);
return versionName;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
/*
* 获取版本号
*/
private int getVersionCode() {
// TODO Auto-generated method stub
PackageManager pm = getPackageManager();
try {
PackageInfo packageInfo = pm.getPackageInfo(getPackageName(), 0);
int versionCode = packageInfo.versionCode;
return versionCode;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
} }
红色的代码是在上个代码之上添加的心代码。目的主要是:生成一个提示提示窗,用于提醒用户有新的版本。
Android之Splash页面的更多相关文章
- Xamarin.Android之Splash的几种简单实现
对现在的APP软件来说,基本上都会有一个Splash页面,类似大家常说的欢迎页面.启动界面之类的. 正常来说这个页面都会有一些相关的信息,比如一些理念,Logo,版本信息等 下面就来看看在Xamari ...
- Xamarin.Android splash页面瞬间响应_避免APP启动闪白屏
Application和Activity中的onCreate都进行了优化,基本没有耗时操作,但是启动应用之后还是会闪现一下白色背景,然后才进入Splash页面,对比了一下QQ.微信.微博等客户端,点击 ...
- Android 手机卫士1--实现splash页面
1.minSdkVersion.targetSdkVersion.maxSdkVersion.target API level四个数值到底有什么区别? minSdkVersion, maxSdkVer ...
- 项目Splash页面的开发与设计
项目Splash页面的开发与设计 首先建立一个安卓的项目,然后修改manifest.xml文件,修改应用程序的logo和显示名称,效果图如下: 对应的代码如下: 1 <?xml version= ...
- weex splash页面
1.Splash.vue <!-- splash页面 --> <template> <div class="wrap" @focus="ro ...
- Android 开启 WebView 页面 Chrome debug
Android 开启 WebView 页面 Chrome debug WebView debug // 开启 WebView 页面 debug testWebView.setWebContentsDe ...
- Android app Splash页的替代方案
一般的App想要显示公司的log什么的,都会在启动的第一个页面显示,就是SplashActivity. 目前在看到一个替代SplashActivity的方案. 使用SplashActivity的时候, ...
- Ionic Android项目Splash设置
ionic项目中,在splashscreen消失后会出现零点几秒的白屏,再出现app页面. 1. 安装Cordova splash screen插件 ionic plugin add org.apac ...
- Android WebView 优化页面加载效果
目前带有Web功能的APP越来越多,为了能够更好的使用WebView展示页面,可以考虑做相关的优化:WebView 缓存,资源文件本地存储,客户端UI优化. 可能有些人会说,为什么不做Native的, ...
随机推荐
- java设计模式(二)---工厂方法模式
2普通工厂方法模式 就是建立一个工厂类,对实现了同一接口的一些类进行实例的创建. 2.1创建接口 /** * 发送接口 * Created by mrf on 2016/2/25. */ public ...
- C#抓取天气数据
使用C#写的一个抓取天气数据的小工具,使用正则匹配的方式实现,代码水平有限,供有需要的同学参考.压缩包中的两个sql语句是建表用的. http://files.cnblogs.com/files/yu ...
- git入门到熟练使用
最近以为接触ios开发,所以对git也产生了一点兴趣.所以在网上搜索资料开始学习,但大部分都是没用的copy的文章,有一个还不错的,推荐给大家 http://www.liaoxuefeng.com/w ...
- Lambda表达式演变
Lambda表达式是一种匿名函数. 演变步骤: 一般的方法委托 => 匿名函数委托 => Lambda表达式 Lambda表达式其实并不陌生,他的前生就是匿名函数,所以要谈La ...
- Unicode中文和特殊字符的编码范围
编程中有时候需要用到匹配中文的正则,一般用 [ \u4e00-\u9fa5]+ 即可搞定.不过这正则对一般的火星文鸟语就不太适用了,甚至全角的标点符号都不包含在内.例如游戏里面的玩家名,普通青年一般都 ...
- SQLProfiler_SQL抓包
有时候我们的某个程序或者应用在执行SQL语句时报错了, 我们需要拿到报错的SQL语句检查, 那么你可以借助:SQL Profiler工具来实现. 1.SQL Profiler是一个可以检测SQL服务器 ...
- iOS的QuickTime Plugin
当UIWebView播放视频时,可以看到view hierarchy里有FigPluginView的身影.这个类来自于QuickTime Plugin,plugin的路径为: /Application ...
- iOS 阶段学习第23天笔记(XML数据格式介绍)
iOS学习(OC语言)知识点整理 一.XML数据格式介绍 1)概念:xml是extensible markup language扩展的标记语言,一般用来表示.传输和存储数据 2)xml与json目前使 ...
- Python语言and-or的用法
[原]python语言的 and-or 常常被用来实现类C语言中的三元运算符 : ? , 更为骚气的写法是 xxx and xxx or xxx and xxx or xxx,这样就可以可以做到 ...
- ahjesus 单词单数-复数相互转换C#
看codesmith内置的模板在生成存储过程的时候有单复数的转换,用相同的函数名实现了一个 public static class StringUtil { /// <summary> / ...