zl程序教程

您现在的位置是:首页 >  APP

当前栏目

[android] 手机卫士欢迎页检测更新

2023-02-18 15:47:26 时间

客户端:去服务器获取最新的版本信息

 

服务器端:

版本信息,最新的版本2.0

最新版本的下载地址:http://xxxxxxxx/mobilesafe2.0.apk

版本的描述信息

 

客户端如果不升级新版本就进入主界面,升级新版本就替换安装

服务器端json信息

{"version":"2.0","description":"发现新版本,下载送现金","apkurl":"http://100.66.221.69/mobilesafe2.0.apk"}

 

联网请求数据

 

把服务器的地址保存在一个配置文件里面,res/values/config.xml

<string name=”serverurl”>http://xxxxxxxxxxxxxxxx</string>

 

开启子线程检查版本信息

new Thread{}.start(),重写run方法

 

获取Url对象

new获取Url对象,捕获异常错误,获取配置信息数据getString(R.string.serverurl)

调用Url对象的openConnection()方法,获取HttpUrlConnection对象

调用它的各种方法获取到数据

 

更新界面&处理异常

 

解析JSON

获取JSONObject对象,通过new JSONObject(string)构造方法

调用JSONObject对象的get(key)方法,获取值,需要强转

descriptapkurl定义成类的成员变量

 

校验是否有新版本

当前的版本和服务端返回的版本进行判断

版本一致进入主界面

如果不同,弹出升级对话框

 

通过Handler来传递消息

Activity里面定义类的成员变量Handler,使用匿名内部类来实现handler,重写方法handleMessage()

在网络访问的线程里面

获取到Message对象,调用Message.obtain()方法,获取已经存在的不要new

设置Message对象的what属性,设置不同的标记,定义成类常量

调用Handler对象的sendMessage()方法,参数:Message对象

处理信息

handleMessage()方法里面进行处理

switch判断不同的what标记,展示对话框和Toast,跳转到主界面

 

跳转到主页

使用显式意图跳转界面

获取Intent对象,

调用startActivity()方法

关闭当前页面

finish()

 

解决页面跳转太快

在联网之前定义一个开始时间startTime

联网结束定义一个结束时间endTime

相隔的时间如果小于2秒(2000),

线程休息2000减去相隔的毫秒,Thread.sleep()

 

界面跳转的动画

获取AlphaAnimation 对象,通过new出来,构造参数:

0.2f 1.0f 透明度的变化 0.21

调用AlphaAnimation 对象的setDuration()方法,设置时间,参数:毫秒

给界面的根布局定义id,找到这个控件

调用View对象的startAnimation()方法,开始动画,参数:AlphaAnimation 对象

 

 

package com.qingguow.mobilesafe;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.animation.AlphaAnimation;
import android.widget.TextView;
import android.widget.Toast;

import com.qingguow.mobilesafe.utils.StreamTools;

public class SplashActivity extends Activity {
    private static final String TAG = "SplashActivity";
    protected static final int ENTER_HOME = 0;
    protected static final int VERSION_UPDATE = 1;
    protected static final int URL_ERROR = 2;
    protected static final int NETWORK_ERROR = 3;
    protected static final int JSON_ERROE = 4;
    private TextView tv_splash_version;
    private String description;
    private String apkurl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        tv_splash_version = (TextView) findViewById(R.id.tv_splash_version);
        tv_splash_version.setText("版本号" + getVersionName());
        // 检查更新
        checkVersion();
        //界面动画
        AlphaAnimation aa=new AlphaAnimation(0.2f, 1.0f);
        aa.setDuration(1000);
        findViewById(R.id.rl_splash_root).setAnimation(aa);
    }

    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case ENTER_HOME:
                enterHome();
                break;
            case VERSION_UPDATE:
                Toast.makeText(getApplicationContext(), description, 0).show();
                break;
            case URL_ERROR:
                Toast.makeText(getApplicationContext(), "URL错误", 0).show();
                enterHome();
                break;
            case NETWORK_ERROR:
                Toast.makeText(getApplicationContext(), "网络错误", 0).show();
                enterHome();
                break;
            case JSON_ERROE:
                Toast.makeText(getApplicationContext(), "JSON解析错误", 0).show();
                enterHome();
                break;
            
            }
        }

        
    };
    /**
     * 进入主页
     */
    private void enterHome() {
        Intent intent =new Intent(SplashActivity.this,HomeActivity.class);
        startActivity(intent);
        finish();
    };
    /**
     * 检查新版本
     */
    private void checkVersion() {
        new Thread() {
            public void run() {
                long startTime=System.currentTimeMillis();
                Message mes = Message.obtain();
                URL url;
                try {
                    url = new URL(getString(R.string.serverurl));
                    HttpURLConnection conn = (HttpURLConnection) url
                            .openConnection();
                    conn.setRequestMethod("GET");
                    conn.setConnectTimeout(4000);
                    int code = conn.getResponseCode();
                    if (code == 200) {
                        InputStream is = conn.getInputStream();
                        String result = StreamTools.readInputStream(is);
                        JSONObject json = new JSONObject(result);
                        String newVersion = (String) json.get("version");
                        if (newVersion.equals(getVersionName())) {
                            // 进入主界面
                            mes.what = ENTER_HOME;
                        } else {
                            // 版本更新
                            mes.what = VERSION_UPDATE;
                            description=(String) json.get("description");
                            apkurl=(String) json.get("apkurl");
                        }
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                    Log.i(TAG, "URL错误");
                    mes.what = URL_ERROR;
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.i(TAG, "网络连接错误");
                    mes.what = NETWORK_ERROR;
                } catch (JSONException e) {
                    e.printStackTrace();
                    Log.i(TAG, "JSON解析错误");
                    mes.what = JSON_ERROE;
                } finally {
                    //延迟效果
                    long endTime=System.currentTimeMillis();
                    long dTime=endTime-startTime;
                    if(dTime<3000){
                        try {
                            Thread.sleep(3000-dTime);
                        } catch (InterruptedException e) {
                        }
                    }
                    handler.sendMessage(mes);
                }

            };
        }.start();

    }

    // 获得应用版本名称
    private String getVersionName() {
        PackageManager pm = getPackageManager();
        try {
            PackageInfo info = pm.getPackageInfo(getPackageName(), 0);
            return info.versionName;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }
}