android开发之应用检测更新

核心代码:

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值