软件的自动更新一般都与Splash界面绑定在一起, 由于需要维护的软件界面很复杂, 一个Activity中嵌入ViewPager, 并且逻辑比较复杂, 索性重新写一个Activity, 现在的软件都很流行使用Splash界面, 正好与自动更新配套在一起;
在这个自动更新Splash中, 使用到了 动画设置 ,SharedPerference ,pull解析 ,dialog对话框 ,http网络编程 ,handler 等.
注意一个错误 : 已安装具有该名称和不同签名的数据包 , 早上测试人员报告突然出现这个问题, 在开发的时候我直接将eclipse上编译的版本放到了服务器上, 最后出现了这个问题, 开发的时候明明是好的啊, 怎么测试的时候出问题了呢.
编译环境不同, 产生的签名是不一样的, 在eclipse上编译生成 与 正式版本在linux下编译 所产生的 数字签名 是不一样的.
又发现一个BUG : 在弹出更新对话框, 点击确定下载完毕之后会弹出系统自带的替换应用程序对话框, 在这里点取消的话就会一直卡在Splash界面. 设置一个跳转机制解决这个问题.
解决方案 :利用触摸划屏事件, 向左侧划屏100px, 就自动跳转到主界面 , 最后的最终代码已经加上去了
-
-
-
-
-
-
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN :
- touchPositionX0 = (int) event.getX();
- break;
- case MotionEvent.ACTION_UP :
- touchPositionX1 = (int) event.getX();
- if((touchPositionX0 - touchPositionX1) > 100)
- loadMainUI();
- touchPositionX0 = 0;
- touchPositionX1 = 0;
- break;
- }
- return true;
- }
一. 创建Activity
1. 创建Activity大概流程
a. 设置全屏显示.
b. 设置布局, 并在布局中显示当前版本号, 为Splash界面添加动画.
c. 获取当前时间.
d. 获取SharedPerence配置文件.
e. 开启检查版本号线程, 后续的操作都在这个线程中执行.
2. 设置窗口样式
(1) 设置全屏显示
a. 代码实现 : 由于是Splash界面, 这里需要设置成无标题, 并且全屏显示, 注意下面的两行代码需要在setContentView()方法之前调用;
-
- requestWindowFeature(Window.FEATURE_NO_TITLE);
-
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
b. 配置实现 :
- AndroidManifest.xml
- <activity
- android:name="myAcitivty"
- android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />
(2) 关于窗口的其它设置
-
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
-
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
设置窗体始终点亮的配置文件实现
-
- <uses-permission android:name="android.permission.WAKE_LOCK" />
-
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
(3) 屏幕方向设置
a. 配置文件实现
-
- <activity android:name="myAcitivty" android:screenOrientation="landscape" />
-
-
- <activity android:name="myAcitivty" android:screenOrientation="portrait" />
b.
代码实现
-
- setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
-
-
- setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
c. 获取屏幕方向
-
- int orientation = this.getResources().getConfiguration().orientation;
其中的orientation方向可以使 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE 或者 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE .
3. 设置动画
为了更好的用户体验, 这里给Splash界面添加一个动画, 这个动画加给整个界面.
(1) 创建动画
- AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);<span style="white-space:pre"> </span>
- animation.setDuration(2000);<span style="white-space:pre"> </span>
- splash_rl.setAnimation(animation);<span style="white-space:pre"> </span>
创建动画吧: 创建的这个动画是透明度渐变动画, 传入浮点型参数, 0代表完全透明, 1代表不透明, 传入参数代表透明度从完全透明到不透明.
设置时间 : 设置的duration是动画渐变过程所消耗的时间.
设置动画 : 最后使用setAnimation()方法将穿件的动画设置给Splash界面.
(2) 动画常用方法
a. 普通设置
- alphaAnimation.setRepeatCount(5);
- alphaAnimation.setFillAfter(true);
- alphaAnimation.setStartOffset(1000);
- alphaAnimation.start();
b. 设置监听器
- alphaAnimation.setAnimationListener(new AnimationListener() {
-
- @Override
- public void onAnimationStart(Animation animation) {
- }
-
- @Override
- public void onAnimationRepeat(Animation animation) {
- }
-
- @Override
- public void onAnimationEnd(Animation animation) {
- }
- });
4. SharedPerference使用
-
- SharedPreferences sharedPreferences = getSharedPreferences("sp", Context.MODE_PRIVATE);
-
- Editor editor = sharedPreferences.edit();
- editor.putBoolean("isUpdate", true);
- editor.commit();
-
- sharedPreferences.getBoolean("isUpdate", true);
5. onCreate()方法代码
-
-
-
-
-
-
-
-
-
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- requestWindowFeature(Window.FEATURE_NO_TITLE);
-
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
-
- setContentView(R.layout.splash);
-
-
-
-
-
- tv_version = (TextView) findViewById(R.id.tv_version);
- version =getString(R.string.current_version) + " " + getVersion();
- tv_version.setText(version);
-
-
-
-
-
-
-
- splash_rl = (RelativeLayout) findViewById(R.id.splash_rl);
- AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
- animation.setDuration(2000);
- splash_rl.setAnimation(animation);
-
-
-
-
-
-
-
- time = System.currentTimeMillis();
-
-
- sp = getSharedPreferences("config", Context.MODE_PRIVATE);
-
-
- new Thread(new CheckVersionTask()).start();
- }
二. 检查版本号
1. 检查版本号线程
流程 :
a. 保持Splash持续时间 : 获取当前时间与time进行比较, 如果不足3秒, 人为使Splash保持3秒时间;
b. 查看更新设置 : 从sp中获取更新设置, 如果sp中自动更新为true, 那么就执行下面的更新流程, 如果sp中自动更新为false, 那么直接进入主界面.
c. 获取信息 : 从网络中获取更新信息, 根据是否成功获取信息执行不同的操作.
源码 :
- private final class CheckVersionTask implements Runnable{
- public void run() {
- try {
-
-
-
-
- long temp = System.currentTimeMillis();
- if(temp - time < 3000){
- SystemClock.sleep(temp - time);
- }
-
-
-
-
-
-
- boolean is_auto_update = sp.getBoolean("is_auto_update", true);
- if(!is_auto_update){
- loadMainUI();
- return;
- }
-
-
-
-
-
-
-
- updateInfo = getUpdateInfo(SettingsFactory.readWebLoadUrl(getApplicationContext()) + UPDATE_FOLDER_DIRECTORY + XML_FILE_DIRECTORY);
- if(updateInfo != null){
- Message msg = new Message();
- msg.what = SUCESS_GET_UPDATEINOF;
- mHandler.sendMessage(msg);
- }else{
- Message msg = new Message();
- msg.what = ERROR_GET_UPDATEINOF;
- mHandler.sendMessage(msg);
- }
- } catch (Exception e) {
- e.printStackTrace();
- Message msg = new Message();
- msg.what = ERROR_GET_UPDATEINOF;
- mHandler.sendMessage(msg);
- }
- }
- }
2. 获取版本号方法
流程 :
a. 创URL建对象;
b. 创建HttpURLConnection对象;
c. 设置超时时间;
d. 设置获取方式;
e. 查看链接是否成功;
f. 解析输入流信息;
源码 :
-
-
-
-
-
-
-
-
-
-
- private UpdateInfo getUpdateInfo(String path){
- try {
- URL url = new URL(path);
-
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
-
- conn.setConnectTimeout(5000);
-
- conn.setRequestMethod("GET");
-
- if(conn.getResponseCode() == 200){
- InputStream is = conn.getInputStream();
-
- return parserUpdateInfo(is);
- }
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (ProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
3. 更新信息对象
将从网上获取的更新信息 包括 版本号, apk文件地址, 软件描述等信息封装在一个类中.
- public class UpdateInfo {
- private String version;
- private String url;
- private String description;
-
- public String getVersion() {
- return version;
- }
- public void setVersion(String version) {
- this.version = version;
- }
- public String getUrl() {
- return url;
- }
- public void setUrl(String url) {
- this.url = url;
- }
- public String getDescription() {
- return description;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- @Override
- public String toString() {
- return "UpdateInfo [version=" + version + ", url=" + url
- + ", description=" + description + "]";
- }
- }
4. pull解析输入流
(1) pull解析流程
a. 获取pull解析器 : XmlPullParser parser = Xml.newPullParser();
b. 为pull解析器设置编码 : parser.setInput(inputStream, "UTF-8");
c. 获取pull解析器事件 : int eventType = parser.getEventType(), 之后的解析都要根据这个解析事件进行, 例如开始解析标签的事件时 XmlPullParser.START_TAG, 文档结束的事件时 XmlPullParser.END_DOCUMENT.
d. 解析流程控制 : 解析的时候, 如果没有解析到文档最后就一直解析, 这里使用while循环, eventType != XmlPullParser.END_DOCUMENT 就一直循环, 循环玩一个元素之后, 调用parser.next()遍历下一个元素.
e. 获取标签名 : 在事件解析标签的时候 ( eventType == XmlPullParser.START_TAG ) , 调用parser.getName()可以获取这个标签的标签名, 如果我们想要获取这个标签下的文本元素, 可以使用parser.nextText()来获取.
(2) 更新xml文件
- <?xml version="1.0" encoding="UTF-8"?>
- <updateInfo>
- <version>3.2</version>
- <url>http:
- <description>客户端更新</description>
- </updateInfo>
(3) 源码
-
-
-
-
-
-
-
- private UpdateInfo parserUpdateInfo(InputStream is){
- try {
- UpdateInfo updateInfo = null;
-
- XmlPullParser parser = Xml.newPullParser();
-
- parser.setInput(is, "UTF-8");
-
- int eventType = parser.getEventType();
-
- while (eventType != XmlPullParser.END_DOCUMENT) {
- switch (eventType) {
-
- case XmlPullParser.START_TAG:
- if ("updateInfo".equals(parser.getName())) {
-
- updateInfo = new UpdateInfo();
- } else if ("version".equals(parser.getName())) {
-
- updateInfo.setVersion(parser.nextText());
- } else if ("url".equals(parser.getName())) {
-
- updateInfo.setUrl(parser.nextText());
- } else if ("description".equals(parser.getName())) {
-
- updateInfo.setDescription(parser.nextText());
- }
- break;
- default:
- break;
- }
-
- eventType = parser.next();
- }
- is.close();
- return updateInfo;
- } catch (XmlPullParserException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
三. Handler对象
Handler对象用来控制整个更新过程的进行;
- private Handler mHandler = new Handler(){
- public void handleMessage(android.os.Message msg) {
- switch (msg.what) {
-
-
-
-
- case ERROR_GET_UPDATEINOF:
- ToastHint.getInstance().showHint(R.string.fail_to_get_updateinfo);
- loadMainUI();
- break;
-
-
-
-
-
- case SUCESS_GET_UPDATEINOF:
- if(updateInfo.getVersion().equals(version)){
- loadMainUI();
- }else{
- showUpdateDialog();
- }
- break;
-
-
-
-
- case ERROR_DOWNLOAD_APK:
- mPb.dismiss();
- ToastHint.getInstance().showHint(R.string.fail_to_get_apk);
- loadMainUI();
- break;
-
-
-
-
- case SUCCESS_DOWNLOAD_APK:
- mPb.dismiss();
- installApk();
- break;
- default:
- break;
- }
- };
- };
四. 下载安装apk文件
1. 更新对话框
(1) 更新流程
先弹出更新对话框提示, 点击确定就弹出进度条对话框, 下载apk文件 . 如果点击取消, 直接进入主界面
更新对话框 : 这是一个AlertDialog , 先创建builder, 然后设置标题, 显示内容, 设置积极消极按钮, 创建对话框 之后显示对话框;
进度条对话框 : 这是一个ProgressDialog, 直接使用new创建, 设置信息与显示样式, 最后显示对话框.
(2) 创建对话框流程
创建一个对话框的流程 :
a. 创建builder对象 : Builder builder = new Builder(context);
b. 设置标题 : builder.setTittle("");
c. 设置显示信息 : builder.setMessage("");
d. 设置按钮 : builder.setPositiveButton("", onClickListener);
e. 创建对话框 : Dialog dialog = builder.create();
f. 显示对话框 : dialog.show();
创建进度条对话框流程 :
a. 创建进度条对话框 : ProgressDialog progressDialog = new ProgressDialog(context);
b. 设置进度条对话框样式 : progressDialog.setProgressStyle();
c. 设置显示信息 : progressDialog.setMessage();
d. 显示对话框 : progressDialog.show();
(3) 源码
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- protected void showUpdateDialog() {
-
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
-
- builder.setTitle(getString(R.string.update_dialog_tittle));
-
- builder.setMessage(updateInfo.getDescription());
-
- builder.setCancelable(false);
-
- builder.setPositiveButton(getString(R.string.confirm), new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
-
- mPb = new ProgressDialog(SplashActivity.this);
-
- mPb.setCancelable(false);
-
- mPb.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
-
- mPb.setMessage(getString(R.string.update_dialog_messsage));
-
- mPb.show();
-
- new Thread(new DownloadApkTask()).start();
- }
- });
- builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- loadMainUI();
- }
- });
-
- mUpdateInfoDialog = builder.create();
-
- mUpdateInfoDialog.show();
- }
2. 下载apk线程
-
-
-
-
- private final class DownloadApkTask implements Runnable{
- public void run() {
- if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
- try {
- SystemClock.sleep(2000);
- apkFile = downloadApk(SettingsFactory.readWebLoadUrl(getApplicationContext()) + UPDATE_FOLDER_DIRECTORY + updateInfo.url,mPb);
- Message msg = new Message();
- msg.what = SUCCESS_DOWNLOAD_APK;
- mHandler.sendMessage(msg);
- } catch (Exception e) {
- e.printStackTrace();
- Message msg = new Message();
- msg.what = ERROR_DOWNLOAD_APK;
- mHandler.sendMessage(msg);
- }
- }
- }
- }
3. 下载apk核心方法
从网络下载文件流程 :
a. 创建URL对象 : 这个对象一般根据字符串地址创建, URL url = new URL(path);
b. 创建HttpURLConnection对象 : 这个对象根据URL对象创建, HttpURLConnection conn = (HttpURLConnection)url.openConnection();
c. 设置超时时间 : 单位是毫秒, conn.setConnectionTimeout(5000);
d. 设置请求方式 : conn.setRequestMethod("GET");
e. 成功连接 : 如果成功连接, 那么conn.getResponseCode()的值为200;
进度条对话框设置 :
a. 设置进度条最大值 : mProgressDialog.setMax(int max);
b. 设置进度条当前值 : mProgressDialog.setProgress(int curr);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public File downloadApk(String path,ProgressDialog pb) throws Exception{
-
- File file = new File(Environment.getExternalStorageDirectory(), getFileName(path));
-
- URL url = new URL(path);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setConnectTimeout(5000);
- conn.setRequestMethod("GET");
- if(conn.getResponseCode() == 200){
- int max = conn.getContentLength();
-
- pb.setMax(max);
- int count = 0;
- InputStream is = conn.getInputStream();
- FileOutputStream fos = new FileOutputStream(file);
- byte[] buffer = new byte[1024];
- int len = 0;
- while((len = is.read(buffer)) != -1){
- fos.write(buffer, 0, len);
-
- count = count + len;
- pb.setProgress(count);
- }
- is.close();
- fos.close();
- }
- return file;
- }
4. 安装apk文件
-
-
-
-
-
-
-
- protected void installApk() {
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_VIEW);
- intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
- startActivity(intent);
- }
五. 相关的源码
(1) 布局文件
splash.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/ivt_splash"
- android:id="@+id/splash_rl">
-
- <ProgressBar android:id="@+id/pb"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_alignParentBottom="true"
- android:layout_marginBottom="30dip"/>
-
- <TextView android:id="@+id/tv_version"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_above="@id/pb"
- android:layout_marginBottom="60dip"
- android:textSize="30sp"
- android:textColor="#17A6E8"
- android:text="version"
- />
- </RelativeLayout>
(2) Activity页面切换动画
main_in.xml
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- >
- <translate
- android:fromXDelta="100%p"
- android:toXDelta="0"
- android:fromYDelta="0"
- android:toYDelta="0"
- android:duration="200"
- />
- </set>
splash_out.xml
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- >
- <translate
- android:fromXDelta="0"
- android:toXDelta="-100%p"
- android:fromYDelta="0"
- android:toYDelta="0"
- android:duration="200"
- />
- </set>
(3) SplashActivity源码
SplashActivity.java
- public class SplashActivity extends Activity {
-
- private static final String TAG = "SplashActivity";
-
- public static final int ERROR_GET_UPDATEINOF = 0;
- public static final int SUCESS_GET_UPDATEINOF = 1;
- public static final int ERROR_DOWNLOAD_APK = 2;
- public static final int SUCCESS_DOWNLOAD_APK = 3;
-
- private static final String XML_FILE_DIRECTORY = "updateinfo.xml";
- private static final String UPDATE_FOLDER_DIRECTORY = "/webupdate/";
-
- private TextView tv_version;
- private PackageManager pm;
- private String version;
- private UpdateInfo updateInfo;
-
- private Dialog mUpdateInfoDialog;
- private ProgressDialog mPb;
- private File apkFile;
-
- private RelativeLayout splash_rl;
- private long time;
- private SharedPreferences sp;
-
- private int touchPositionX0;
- private int touchPositionX1;
-
- private Handler mHandler = new Handler(){
- public void handleMessage(android.os.Message msg) {
- switch (msg.what) {
-
-
-
-
- case ERROR_GET_UPDATEINOF:
- ToastHint.getInstance().showHint(R.string.fail_to_get_updateinfo);
- loadMainUI();
- break;
-
-
-
-
-
- case SUCESS_GET_UPDATEINOF:
- if(updateInfo.getVersion().equals(version)){
- loadMainUI();
- }else{
- showUpdateDialog();
- }
- break;
-
-
-
-
- case ERROR_DOWNLOAD_APK:
- mPb.dismiss();
- ToastHint.getInstance().showHint(R.string.fail_to_get_apk);
- loadMainUI();
- break;
-
-
-
-
- case SUCCESS_DOWNLOAD_APK:
- mPb.dismiss();
- installApk();
- break;
- default:
- break;
- }
- };
- };
-
-
-
-
-
-
-
-
-
-
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- requestWindowFeature(Window.FEATURE_NO_TITLE);
-
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
-
- setContentView(R.layout.splash);
-
-
-
-
-
- tv_version = (TextView) findViewById(R.id.tv_version);
- version =getString(R.string.current_version) + " " + getVersion();
- tv_version.setText(version);
-
-
-
-
-
-
-
- splash_rl = (RelativeLayout) findViewById(R.id.splash_rl);
- AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
- alphaAnimation.setDuration(2000);
- splash_rl.setAnimation(alphaAnimation);
-
-
-
-
-
-
-
- time = System.currentTimeMillis();
-
-
- sp = getSharedPreferences("config", Context.MODE_PRIVATE);
-
-
- new Thread(new CheckVersionTask()).start();
- }
-
- private final class CheckVersionTask implements Runnable{
- public void run() {
- try {
-
-
-
-
- long temp = System.currentTimeMillis();
- if(temp - time < 3000){
- SystemClock.sleep(temp - time);
- }
-
-
-
-
-
-
- boolean is_auto_update = sp.getBoolean("is_auto_update", true);
- if(!is_auto_update){
- loadMainUI();
- return;
- }
-
-
-
-
-
-
-
- updateInfo = getUpdateInfo(SettingsFactory.readWebLoadUrl(getApplicationContext()) + UPDATE_FOLDER_DIRECTORY + XML_FILE_DIRECTORY);
- if(updateInfo != null){
- Message msg = new Message();
- msg.what = SUCESS_GET_UPDATEINOF;
- mHandler.sendMessage(msg);
- }else{
- Message msg = new Message();
- msg.what = ERROR_GET_UPDATEINOF;
- mHandler.sendMessage(msg);
- }
- } catch (Exception e) {
- e.printStackTrace();
- Message msg = new Message();
- msg.what = ERROR_GET_UPDATEINOF;
- mHandler.sendMessage(msg);
- }
- }
- }
-
-
-
-
-
-
-
-
- protected void installApk() {
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_VIEW);
- intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
- startActivity(intent);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- protected void showUpdateDialog() {
-
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
-
- builder.setTitle(getString(R.string.update_dialog_tittle));
-
- builder.setMessage(updateInfo.getDescription());
-
- builder.setCancelable(false);
-
- builder.setPositiveButton(getString(R.string.confirm), new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
-
- mPb = new ProgressDialog(SplashActivity.this);
-
- mPb.setCancelable(false);
-
- mPb.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
-
- mPb.setMessage(getString(R.string.update_dialog_messsage));
-
- mPb.show();
-
- new Thread(new DownloadApkTask()).start();
- }
- });
- builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- loadMainUI();
- }
- });
-
- mUpdateInfoDialog = builder.create();
-
- mUpdateInfoDialog.show();
- }
-
-
-
-
-
- private final class DownloadApkTask implements Runnable{
- public void run() {
- if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
- try {
- SystemClock.sleep(2000);
- apkFile = downloadApk(SettingsFactory.readWebLoadUrl(getApplicationContext()) + UPDATE_FOLDER_DIRECTORY + updateInfo.url,mPb);
- Message msg = new Message();
- msg.what = SUCCESS_DOWNLOAD_APK;
- mHandler.sendMessage(msg);
- } catch (Exception e) {
- e.printStackTrace();
- Message msg = new Message();
- msg.what = ERROR_DOWNLOAD_APK;
- mHandler.sendMessage(msg);
- }
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public File downloadApk(String path,ProgressDialog pb) throws Exception{
-
- File file = new File(Environment.getExternalStorageDirectory(), getFileName(path));
-
- URL url = new URL(path);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setConnectTimeout(5000);
- conn.setRequestMethod("GET");
- if(conn.getResponseCode() == 200){
- int max = conn.getContentLength();
-
- pb.setMax(max);
- int count = 0;
- InputStream is = conn.getInputStream();
- FileOutputStream fos = new FileOutputStream(file);
- byte[] buffer = new byte[1024];
- int len = 0;
- while((len = is.read(buffer)) != -1){
- fos.write(buffer, 0, len);
-
- count = count + len;
- pb.setProgress(count);
- }
- is.close();
- fos.close();
- }
- return file;
- }
-
- private String getFileName(String path){
- return path.substring(path.lastIndexOf("/") + 1);
- }
-
- private String getVersion() {
- try {
- pm = this.getPackageManager();
- PackageInfo packageInfo = pm.getPackageInfo(getPackageName(), 0);
- return packageInfo.versionName;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
-
-
-
-
-
-
-
-
-
-
-
- private UpdateInfo getUpdateInfo(String path){
- try {
- URL url = new URL(path);
-
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
-
- conn.setConnectTimeout(5000);
-
- conn.setRequestMethod("GET");
-
- if(conn.getResponseCode() == 200){
- InputStream is = conn.getInputStream();
-
- return parserUpdateInfo(is);
- }
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (ProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
-
-
-
-
-
-
-
-
- private UpdateInfo parserUpdateInfo(InputStream is){
- try {
- UpdateInfo updateInfo = null;
-
- XmlPullParser parser = Xml.newPullParser();
-
- parser.setInput(is, "UTF-8");
-
- int eventType = parser.getEventType();
-
- while (eventType != XmlPullParser.END_DOCUMENT) {
- switch (eventType) {
-
- case XmlPullParser.START_TAG:
- if ("updateInfo".equals(parser.getName())) {
-
- updateInfo = new UpdateInfo();
- } else if ("version".equals(parser.getName())) {
-
- updateInfo.setVersion(parser.nextText());
- } else if ("url".equals(parser.getName())) {
-
- updateInfo.setUrl(parser.nextText());
- } else if ("description".equals(parser.getName())) {
-
- updateInfo.setDescription(parser.nextText());
- }
- break;
- default:
- break;
- }
-
- eventType = parser.next();
- }
- is.close();
- return updateInfo;
- } catch (XmlPullParserException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
-
- private void loadMainUI(){
- Intent intent = new Intent(this,HomeActivity.class);
- startActivity(intent);
- finish();
- overridePendingTransition(R.anim.main_in, R.anim.splash_out);
- }
-
-
- public class UpdateInfo {
- private String version;
- private String url;
- private String description;
-
- public String getVersion() {
- return version;
- }
- public void setVersion(String version) {
- this.version = version;
- }
- public String getUrl() {
- return url;
- }
- public void setUrl(String url) {
- this.url = url;
- }
- public String getDescription() {
- return description;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- @Override
- public String toString() {
- return "UpdateInfo [version=" + version + ", url=" + url
- + ", description=" + description + "]";
- }
- }
-
-
-
-
-
-
-
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN :
- touchPositionX0 = (int) event.getX();
- break;
- case MotionEvent.ACTION_UP :
- touchPositionX1 = (int) event.getX();
- if((touchPositionX0 - touchPositionX1) > 100)
- loadMainUI();
- touchPositionX0 = 0;
- touchPositionX1 = 0;
- break;
- }
- return true;
- }
- }