@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>