您的位置:首页 > 移动开发 > Android开发

android开发之应用检测更新

2015-02-27 17:38 323 查看
核心代码:

class GetDATA_Task extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
Sign_Tool sign = new Sign_Tool();
ArrayList<String> vallist = new ArrayList<String>();
vallist.add("8"); //app的序号,根据你公司的安排
vallist.add("app_version"); //app更新的关键字,由你公司规定
vallist.add("android"); //app的所属范围
String sign_str = sign.getSign(vallist);
url = "http://app.com/phone_api/return_url2.php?app=app的序号&request_type=app更新的关键字&s=app的所属范围&sign="+sign_str; //url由你公司规定
result = HTTP_Tool.getData(url);
return result;
}

@Override
protected void onPostExecute(String result) {
if (result != null && !result.equals("") && !result.equals("false")) {
try {
JSONTokener jsonParser = new JSONTokener(result);
JSONObject person = (JSONObject) jsonParser.nextValue();
appversion = person.getString("version");
newappurl = person.getString("url");
} catch (JSONException ex) {
// 异常处理代码
}
}
update_App_Version();
}
}

public void update_App_Version() {
if (appversion != null && !appversion.equals("")) {
if (!appversion.equals(getVersionName())) {
if (newappurl != null && !newappurl.equals("")) {
LinearLayout ll;
ImageView yes, no;
appUpdate = new Pop_Setting_AppUpdate(IndexActivity.this);
tv = (TextView) appUpdate.mDialog.findViewById(R.id.appupdate_ok_text);
appUpdate.show();
ll = (LinearLayout) appUpdate.mDialog.findViewById(R.id.appupdate_ok_menu);
ll.setVisibility(View.VISIBLE);
tv.setText(R.string.setting_updateing_ok);
yes = (ImageView) appUpdate.mDialog.findViewById(R.id.s_appupdate_ok_yes);
no = (ImageView) appUpdate.mDialog.findViewById(R.id.s_appupdate_ok_no);
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent it = new Intent(Intent.ACTION_VIEW);
it.setData(Uri.parse(newappurl));
startActivity(it);
appUpdate.dismiss();
}
});
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
appUpdate.dismiss();
}
});
}
}else {
if(!isCancel) {
popUpdate();
}
}
} else {
if(!isCancel) {
popUpdate();
}
}
}

private void popUpdate() {
TextView tv = (TextView) appUpdate.mDialog.findViewById(R.id.appupdate_ok_text);
tv.setText("  您当前已是最新版本  ");
new Handler().postDelayed(new Runnable() {

@Override
public void run() {
appUpdate.dismiss();
}
}, 2000);
}


Sign_Tool 工具类:

public class Sign_Tool {

String signkey = "offcn8999|", keycode = ""; //由公司规定:offcn8999|
private static char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

public String getSign(ArrayList<String> vallist) {

for (String val : vallist) {
keycode = keycode + signkey + val;
}

if (keycode.equals("")) {
return null;
}
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.update(keycode.getBytes());
return getFormattedText(messageDigest.digest());
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public static String getMD5(String s) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.update(s.getBytes());
return getFormattedText(messageDigest.digest());
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private static String getFormattedText(byte[] bytes) {
int len = bytes.length;
StringBuilder buf = new StringBuilder(len * 2);
// 把密文转换成十六进制的字符串形式
for (int j = 0; j < len; j++) {
buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
}
return buf.toString();
}
}


HTTP_Tool工具类:

public class HTTP_Tool {

/**
* 通过HttpGet获取行情接口返回的数据
* */
public static String getData(String url) {
String result = null;
try {
// GET的URL,参数直接加URL后
HttpGet httpget = new HttpGet(url);
// 建立HttpPost对象
HttpResponse response = new DefaultHttpClient().execute(httpget);
// 发送GET,并返回一个HttpResponse对象
result = EntityUtils.toString(response.getEntity());
// 得到返回的字符串
} catch (Exception e) {
e.printStackTrace();
}
if (result == null) {
return "";
}
return result.trim();
}
}


Pop_Setting_AppUpdate类:

public class Pop_Setting_AppUpdate {

public Dialog mDialog;

public Dialog getmDialog() {
return mDialog;
}

public void setmDialog(Dialog mDialog) {
this.mDialog = mDialog;
}

public Pop_Setting_AppUpdate(Activity context) {
mDialog = new Dialog(context, R.style.Setting_User_login_Ok_Dialog_Style);

mDialog.setContentView(R.layout.setting_appupdate_ok);
Window window = mDialog.getWindow();
WindowManager wm = context.getWindowManager();
Display d = wm.getDefaultDisplay(); // 获取屏幕宽、高用
WindowManager.LayoutParams p = window.getAttributes();
p.width = (int) (d.getWidth() * 0.85);
window.setAttributes(p);
mDialog.setFeatureDrawableAlpha(Window.FEATURE_OPTIONS_PANEL, 0);
window.setWindowAnimations(android.R.anim.fade_in);
}

public void show() {
mDialog.show();
}

public void dismiss() {
mDialog.dismiss();
}
}


Setting_User_login_Ok_Dialog_Style样式:

<style name="Setting_User_login_Ok_Dialog_Style" parent="@android:style/Theme.Dialog">

<!-- <item name="android:windowFrame">@null</item> -->
<!-- <item name="android:windowIsFloating">true</item> -->
<!-- <item name="android:windowIsTranslucent">false</item> -->
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@null</item>
<!-- <item name="android:backgroundDimEnabled">false</item> -->
</style>


setting_appupdate_ok.xml布局文件:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">

<TextView
android:id="@+id/appupdate_ok_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/s_35dp"
android:layout_marginLeft="@dimen/s_40dp"
android:layout_marginRight="@dimen/s_40dp"
android:layout_marginTop="@dimen/s_35dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="@dimen/s_16dp" />

<LinearLayout
android:id="@+id/appupdate_ok_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="@dimen/s_35dp"
android:orientation="horizontal"
android:visibility="gone">

<ImageView
android:id="@+id/s_appupdate_ok_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/btn_cancle" />

<ImageView
android:id="@+id/s_appupdate_ok_yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/s_20dp"
android:src="@drawable/btn_sure" />
</LinearLayout>
</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 应用更新