• 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;
        }
       
        
    }

    红色的代码是在上个代码之上添加的心代码。目的主要是:生成一个提示提示窗,用于提醒用户有新的版本。

  • 相关阅读:
    opendressinghash //use resize array
    ChainingHash
    Hash function
    stack && queue
    random_select
    counting sort
    master theorem
    各排序算法及其比较
    视图中添加主键的方法
    oracle表空间的扩展
  • 原文地址:https://www.cnblogs.com/kangyaping/p/5495453.html
Copyright © 2020-2023  润新知