版本升级

    private Handler handler = new Handler() {
           @Override
           public void handleMessage(Message msg) {
               super.handleMessage(msg);
               try {
                   int versionCode = SecondActivity.this.getPackageManager().getPackageInfo(SecondActivity.this.getPackageName(), 0).versionCode;
                   if (versionCode < ub.version) {
                       View view = View.inflate(SecondActivity.this, R.layout.updete_dialog, null);
                       final AlertDialog dialog = new AlertDialog.Builder(SecondActivity.this).
                               setView(view).
                               create();
                       final TextView banben = (TextView) view.findViewById(R.id.banben_update);
                       banben.setText("版本号:"+versionCode+" -> "+ub.version);
                      
                       Button no = (Button) view.findViewById(R.id.no_update);
                       no.setOnClickListener(new View.OnClickListener() {
                           @Override
                           public void onClick(View v) {
                               dialog.dismiss();
                           }
                       });
                       Button yes = (Button) view.findViewById(R.id.yes_update);
                       yes.setOnClickListener(new View.OnClickListener() {
                           @Override
                           public void onClick(View v) {
                               Toast.makeText(SecondActivity.this, "正在下载...", Toast.LENGTH_SHORT).show();
                               dialog.dismiss();
                           }
                       });
                       dialog.show();
                   }
               } catch (PackageManager.NameNotFoundException e) {
                   e.printStackTrace();
               }
           }

       };



//线程获取数据

    new Thread() {
               public void run() {
                   super.run();
                   try {
                       URL url = new URL("http://169.254.133.228:8080/goods_file/update_file.xml");
                       HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                       connection.setReadTimeout(5000);
                       connection.connect();
                       if (connection.getResponseCode() == 200) {
                           InputStream inputStream = connection.getInputStream();
                           XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                           XmlPullParser parser = factory.newPullParser();
                           parser.setInput(inputStream, "utf-8");
                           int type = parser.getEventType();
                           while (type != XmlPullParser.END_DOCUMENT) {
                               String name = parser.getName();
                               switch (type) {
                                   case XmlPullParser.START_TAG:
                                       if (name.equals("version")) {
                                           ub.version = Integer.parseInt(parser.nextText());
                                       } else if (name.equals("url")) {
                                           ub.url = parser.nextText();
                                       } else if (name.equals("description")) {
                                           ub.description = parser.nextText();
                                       }
                                       break;
                               }
                               type = parser.next();
                           }
                           handler.sendEmptyMessage(0);
                       }
                   } catch (MalformedURLException e) {
                       e.printStackTrace();
                   } catch (IOException e) {
                       e.printStackTrace();
                   } catch (XmlPullParserException e) {
                       e.printStackTrace();
                   }
               }
           }.start();



//布局

 <LinearLayout
        android:layout_width="300dp"
        android:layout_height="200dp"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginTop="10dp"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/banben_update"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:textColor="#2B2B2B" />

       

        <RelativeLayout
            android:layout_width="230dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp">

            <Button
                android:id="@+id/no_update"
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:layout_marginLeft="8dp"
                android:background="#ccc"
                android:text="取消升级"
                android:textColor="#fff"
                android:textSize="16sp" />

            <Button
                android:id="@+id/yes_update"
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:layout_alignParentRight="true"
                android:layout_marginRight="8dp"
                android:background="@android:color/holo_green_light"
                android:text="立即升级"
                android:textSize="16sp" />

        </RelativeLayout>

    </LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值