手机安全卫士_spalsh页面01

来源:互联网 发布:手机淘宝推广软件 编辑:程序博客网 时间:2024/06/06 02:05
<style name= "AppTheme" parent ="AppBaseTheme">
       <item name= "android:windowNoTitle" >true</item> //设置没有标题,背下来,没有联想功能
    </style >
/*
 *可以用这方法获取包的一些信息,如:版本名称,版本号...
*/
private PackageManager pm ;//包管理者
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout. activity_spalsh);
     tv_spalsh_version = (TextView) findViewById(R.id.tv_spalsh_version );
     pm = getPackageManager();
      PackageInfo pi = pm.getPackageInfo(getPackageName(), 0);//包的信息
       tv_spalsh_version.setText(pi.versionName );//获得包的版本名称
打开快捷键      CTRL+N
private void checkUpdate() {
     new Thread( new Runnable() {  //网络连接过程必须在子线程中使用,主线程上就报错          
           @Override
           public void run() {
              URL url = new URL(getResources().getString(R.string.update_url ));//使用xml中的update_url用于修改
              HttpURLConnection conn = (HttpURLConnection) url.openConnection();
              conn.setConnectTimeout(3000);
              conn.setRequestMethod( "GET");//网络连接模板     
              if (200 == conn.getResponseCode()) {  //200表示连接成功
                    InputStream input = conn.getInputStream();
                    String jsonStr = StreamUtils.convertStream2String(input);  //将字符流转换为字符串,用一个工具类表示出来
                    System. out.println(jsonStr);                   
          }
     }).start();   
}

<?xml version= "1.0" encoding ="utf-8"?>
<resources>
    <string name="update_url" >http://192.168.56.1:8080/ms39/update.json </string> //配置文件,方便增删改查
</resources>

update.json
{code:2,name:"中秋巨献版",url:"http://192.168.56.1:8080/ms39/update",desc:"手机卫士,中秋巨献,快来下载吧,下载送月饼,月饼找博主要."}
//格式一定要正确,不然会出错

<uses-permission android:name ="android.permission.INTERNET"/>  //清单文件上要设置网络权限

public class StreamUtils {      //字符流转换为字符串的工具类
     public static String convertStream2String(InputStream input){         
           ByteArrayOutputStream baos = new ByteArrayOutputStream();//固定写法
           try {              
              byte[] buffer  =new byte[1024];
               int len=-1;            
               while((len=input.read(buffer))!=-1){
                   baos.write(buffer, 0, len);//字符流写入的三部曲
              }            
          } catch (IOException e) {
              e.printStackTrace();
          }  
           return new String(baos.toByteArray());
     }
}
public void run() {
     Message msg = Message. obtain();//主线程与子线程连接的中间变量,固定格式
     try {
          URL url = new URL(getResources().getString(R.string.update_url ));
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();
          conn.setConnectTimeout(3000);
          conn.setRequestMethod( "GET");
          
           if (200 == conn.getResponseCode()) {
              InputStream input = conn.getInputStream();
              String jsonStr = StreamUtils.convertStream2String(input);
              System. out.println(jsonStr);
              JSONObject json = new JSONObject(jsonStr);//获得json对象,记住格式
               int code = json.getInt("code" );  //json相当于键值对,通过键去找值
              String name = json.getString("name");
               desc = json.getString( "desc");
               if (currentCode == code) {   //判断当前版本与json版本是否一致
                   //不需要更新.跳转到主页面
                   msg.what = JUMP2HOME;  //通过msg.what去主线程Handler查找对应的操作
              } else{
                   //弹出提示用户是否升级对话框
                   msg. what = SHOW_CONFIRM_DIALOG;  //通过msg.what去主线程Handler查找对应的操作
              }
          }else{
               //服务器出错
               jump2Home();


          }
    finally{              
              handler.sendMessage(msg);//固定格式,主线程调用子线程
          }
     }
}).start();


private Handler handler = new Handler (){   //没什么好说的,主线程的固定格式,记住就行
     public void handleMessage(Message msg) {
           switch (msg.what ) {
           case SHOW_CONFIRM_DIALOG :
              showConfirmUpdateDialog();
               break;
           case JUMP2HOME :
              jump2Home();
               break;
           default:
               break;
          }
     };
};


protected void showConfirmUpdateDialog() {    //显示 是否确认升级对话框
     AlertDialog.Builder adb = new AlertDialog.Builder(this );
     adb.setTitle( "升级提醒" );   /设置对话框标题
     adb.setMessage( desc);    //设置对话框内容
     adb.setOnCancelListener( new OnCancelListener() {   //设置取消对话框监听机制,相当于点击右上角的x
           @Override
           public void onCancel(DialogInterface dialog) {
               // TODO Auto-generated method stub
              jump2Home();
          }
     });
     adb.setPositiveButton("立刻升级" , new OnClickListener() {    //确认按钮      
           @Override
           public void onClick(DialogInterface dialog, int which) {
               // TODO Auto-generated method stub
              .....
          }
     });
     adb.setNegativeButton("暂不升级" , new OnClickListener() {   //取消按钮
           @Override
           public void onClick(DialogInterface dialog, int which) {
               // TODO Auto-generated method stub
              jump2Home();
          }
     });
     adb.show();       //将对话框展示出来,别忘记了这步
}


protected void jump2Home() {         //跳转至主页面
     // TODO Auto-generated method stub
     Intent intent = new Intent(this ,HomeActivity.class);     //通过反射获取意图对象
     startActivity(intent);    //活动开启
     finish();   //关闭活动  A调用你,就关闭A.适用于一个activity调用另一个activity.
}
AlphaAnimation aa = new AlphaAnimation(0f, 1f);//一个有透明到实体渐变的动画
aa.setDuration(2000);//设置时间
findViewById(R.id.rl_spalsh).startAnimation(aa);//动画开始


protected void downloadFile() {       //下载文件
HttpUtils httputils = new HttpUtils();      //使用了xUtils-2.6.11.jar工具包,要想关联源码,必须先删除自动add to path的jar包,然后手动去add to path,最后在order and export中选中jar包,使它可以在程序中使用
httputils.download(updateUrl, Environment.getExternalStorageDirectory()+ "/temp.apk", new RequestCallBack<File>() {//下载文件,固定写法
     @Override
     public void onSuccess(ResponseInfo<File> responseInfo) {       //下载成功
          System. out.println("文件下载成功" +responseInfo.result.getAbsolutePath());
     }
     
     @Override
     public void onFailure(HttpException error, String msg) {      //下载失败
              jump2Home();
      }
      @Override
      public void onLoading(long total, long current, boolean isUploading) {
          tv_splash_update_info.setText(current+"/" +total);   //下载数字显示的进度      34532/453445
      }
});


要想下载,要在清单文件中添加2个权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

0 0
原创粉丝点击