本帖最后由 今夕何夕 于 2023-3-20 14:34 编辑
1:注册广播配置服务
- /**
- * 注册广播
- */
- private void registerBroadcast() {
- mUpdateReceiver = new UpdateReceiver(false);
- mIntentFilter = new IntentFilter(UpdateReceiver.UPDATE_ACTION);
- this.registerReceiver(mUpdateReceiver, mIntentFilter);
- }
- /**
- * 结束广播
- */
- private void unRegisterBroadcast() {
- try {
- this.unregisterReceiver(mUpdateReceiver);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
复制代码
2、配置更新组件
- private UpdateReceiver mUpdateReceiver;
- private IntentFilter mIntentFilter;
- registerBroadcast();
- checkUpdate();
复制代码
3、checkUpdate
- /**
- * 检查版本更新
- */
- protected void checkUpdate(){
- String url = App.apiHost +"/index/checkVer";
- StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
- @Override
- public void onResponse(String s) {
- try {
- JSONObject info = new JSONObject(s);
- Log.i("TAG",info.toString());
- int status = info.getInt("status");
- if(status == 1) {
- UpdateInformation.serverVersion = Integer.parseInt(info.getString("serverVersion"));
- UpdateInformation.serverFlag = Integer.parseInt(info.getString("serverFlag"));
- UpdateInformation.lastForce = Integer.parseInt(info.getString("lastForce"));
- UpdateInformation.updateurl = info.getString("updateurl");
- UpdateInformation.upgradeinfo = info.getString("upgradeinfo");//升级信息
- UpdateInformation.appname = info.getString("appname");
- sendBroadcast(new Intent(UpdateReceiver.UPDATE_ACTION));
- }
- Log.i("TAG","serverVersion"+UpdateInformation.serverVersion);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- }
- });
- mQueue.add(stringRequest);
- }
复制代码
|