美文网首页Android技术交流
Splash界面的实现过程

Splash界面的实现过程

作者: 猿小v | 来源:发表于2014-11-30 22:04 被阅读885次

    一.splash界面的作用

    1. 用来展现产品的Logo
    2. 应用程序初始化的操作
    3. 检查应用程序的版本
    4. 检查当前应用程序是否合法注册

    二、界面的xml定义

    写一个布局背景设置为产品的logo图片,再添加一个textview显示版本号。

        <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"/>
    

    三、动态获取版本号的方法

    public String getAppVersion(){
        PackageManager pm = getPackageManager();
        try {
            PackageInfo info = pm.getPackageInfo(getPackageName(), 0);
            return info.versionName;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            //不可能发生;
            return "";
        }
    }
    

    四、链接服务器获取更新信息

    1. 升级提醒的对话框

       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();
      
    2. 在子线程中请求服务器的代码 checkup()方法

       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

        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做上下文;

    相关文章

      网友评论

        本文标题:Splash界面的实现过程

        本文链接:https://www.haomeiwen.com/subject/etdgtttx.html