android学习笔记01

 splash闪屏界面:

  1.展示产品的logo 提供产品的知名度.

  2.后台做一些初始化的操作,数据库的拷贝,服务器信息的下载.

    如果发现服务器上有新的版本出现.

  3.判断当前日期和时间,动态的设置splash的界面.

    不是在程序发布的时候 存储到apk包里面的.

  4.获取当前应用程序的版本号:

        packageManager = getPackageManager();

         PackageInfo packinfo = packageManager.getPackageInfo(

                                               getPackageName(), 0);

                            return packinfo.versionName;

  5.splash界面的全屏显示:

         // 取消标题栏

         requestWindowFeature(Window.FEATURE_NO_TITLE);

         // 取消状态栏

         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

         WindowManager.LayoutParams.FLAG_FULLSCREEN);

全屏显示也可以在清单文件中进行配置:

        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

 6.连接服务器获取版本信息:

   流程图.bmp

 

 

 

7. Message msg = Message.obtain(); 复用消息对象   

8.xml文件的解析:

package cn.itcast.mobilesafe.engine;

 

import java.io.InputStream;

 

import org.xmlpull.v1.XmlPullParser;

 

import android.util.Xml;

 

import cn.itcast.mobilesafe.domain.UpdateInfo;

 

public class UpdateInfoParser {

 

         /**

          * 获取输入流里面的更新的信息

          * @param is

          * @return updateinfo

          */

         public static UpdateInfo getUpdateInfo(InputStream is ) throws Exception{

                   XmlPullParser parser =    Xml.newPullParser();

                   parser.setInput(is, "UTF-8");

                   UpdateInfo info = new UpdateInfo();

                   int type = parser.getEventType();

                   while(type!=XmlPullParser.END_DOCUMENT){

                            switch (type) {

                            case XmlPullParser.START_TAG:

                                     if("version".equals(parser.getName())){

                                               String version = parser.nextText();

                                               info.setVersion(version);

                                     }else if("description".equals(parser.getName())){

                                               String description = parser.nextText();

                                               info.setDescription(description);

                                     }else if("url".equals(parser.getName())){

                                               String url = parser.nextText();

                                               info.setUrl(url);

                                     }

                                     break;

 

                            }

                            type = parser.next();

                   }

                   return info;

         }

 

9.splash界面进入主界面一定要调用finish()方法,否则在程序返回的时候还会看到splash

界面

10.http请求:

  String path = getResources().getString(R.string.updateurl);

              URL url = new URL(path);

              HttpURLConnection conn = (HttpURLConnection) url

                     .openConnection();

              conn.setRequestMethod("GET");

              conn.setConnectTimeout(5000);

InputStream is = conn.getInputStream();

              updateInfo = UpdateInfoParser.getUpdateInfo(is);

拿到这个封装好的updateInfo对象

11.弹出升级对话框:

    AlertDialog.Builder builder = new Builder(

                         SplashActivity.this);

                  builder.setTitle("升级提示");

                  builder.setMessage(updateInfo.getDescription());

       builder.setPositiveButton("升级", new OnClickListener()

builder.setNegativeButton("下次", new OnClickListener()

    builder.create().show();

12.子线程的开启:

    new Thread(new CheckVersionTask()).start();

13.方法的抽取:Refactor-Extra Methord

14.splash界面增加一个动画的效果:在整个relative layout界面增加一个id

        AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);

       aa.setDuration(2000);

rl.setAnimation(aa);

15.对话框是activity的一部分,弹出对话框后activity并没有失去焦点,不会执行

     Onpause()方法

16.文件下载

    package cn.itcast.mobilesafe.engine;

 

import java.io.File;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

 

import android.app.ProgressDialog;

import android.os.Environment;

import android.widget.SlidingDrawer;

 

public class DownLoadService {

 

    /**

     * 下载某个path对应的文件

     * @param path

     * @param pd

     * @return 文件对象

     */

    public static File downfile(String path, ProgressDialog pd) throws Exception{

       //http://192.168.1.247:8080/version2_0.apk

       URL url = new URL(path);

       HttpURLConnection conn =  (HttpURLConnection) url.openConnection();

       conn.setConnectTimeout(5000);

       conn.setRequestMethod("GET");

        int length =   conn.getContentLength();

         pd.setMax(length);

      

       InputStream is = conn.getInputStream();

       String filename = getFileName(path);

       File file = new File(Environment.getExternalStorageDirectory(),filename);

       FileOutputStream fos = new FileOutputStream(file);

      

       byte[] buffer = new byte[1024];

       int len=0;

       int total = 0;

       while( (len =is.read(buffer))!=-1){

           fos.write(buffer, 0, len);

           Thread.sleep(10);

           total += len;

           pd.setProgress(total);

       }

       fos.flush();

       fos.close();

       is.close();

       return file;

    }

   

    public static String getFileName(String path){

       int index = path.lastIndexOf("/")+1;

       return path.substring(index, path.length());

    }

}

17.判断sd卡是否存在

(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))

18.激活系统安装APKIntent

Intent intent = new Intent();                                      intent.setAction(Intent.ACTION_VIEW);

intent.setDataAndType(Uri.fromFile(file),     "application/vnd.android.package-archive");

    startActivity(intent);

19.android应用程序没有发布的时候默认使用的是debug.keystore,发布的时候需要使用自己的keystore,签名不同的程序不能够完成替换安装

20.splash里的更新对话框可以在设置中心中进行控制,根据设置来是否弹出更新对话框,这个功能是通过将设置数据持久化到sp中,然后从sp中获取设置数据

本文出自 “蒲公英的梦想” 博客,转载请与作者联系!

你可能感兴趣的:(android,学习笔记01)