版本更新

//请求接口

 @Override
    public void initData() {
        Map<String, Object> map = new HashMap<>();
        map.put("operaterId", getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getInt("userId", -1));
        httpProvider.indexData(INDEX_DATA, URLs.indexData, map);

    }

//请求成功返回参数 比较当前版本和请求的版本

 ApkVersionEntity  apkVersionEntity= (ApkVersionEntity)object;
                apkversion = apkVersionEntity;
                //apkversion = JSONUtils.fromJson(apkVersionEntity.toString(), ApkVersionEntity.class);
                localCode = CommonUtil.getAPPVersionCode();
                serverCode = Integer.parseInt(apkversion.getData().get(0).getVersionCode());
                if (serverCode > localCode){
                    initUpdate();
                }

//获取当前版本号

 /**
     * 获取apk的版本号 currentVersionCode
     *
     * @return
     */
    public static int getAPPVersionCode() {
        int currentVersionCode = 0;
        PackageManager packagemanage = BaseApplication.getInstance().getApplicationContext().getPackageManager();
        try {
            PackageInfo info = packagemanage.getPackageInfo(BaseApplication.getInstance().getApplicationContext().getPackageName(), 0);
            currentVersionCode = info.versionCode; // 版本号
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return currentVersionCode;
    }

//服务器上的版本号大于本地版本号弹框

public void initUpdate() {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View view = inflater.inflate(R.layout.version_upgrade_dialog, null);
        ImageView btn_up = (ImageView) view.findViewById(R.id.btn_up);
        ImageView btn_unup = (ImageView) view.findViewById(R.id.btn_unup);
        TextView up_content = (TextView) view.findViewById(R.id.up_content);
        TextView up_version = (TextView) view.findViewById(R.id.up_version);
        final Dialog dialog = new Dialog(getContext(), R.style.MyDialog);
        dialog.show();
        up_version.setText("V."+apkversion.getData().get(0).getVersionName());
        up_content.setText(apkversion.getData().get(0).getRemark());
        SpUtils.setParam("up_content",apkversion.getData().get(0).getRemark());
        SpUtils.setParam("up_url",apkversion.getData().get(0).getUrl());
        if (dialog.getWindow() != null) {
            dialog.getWindow().setContentView(view);//自定义布局应该在这里添加,要在dialog.show()的后面
        }
        //确定
        btn_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            //直接进入apk地址
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(apkversion.getData().get(0).getUrl())));
                /*new AppUpdater.Builder()
                        .serUrl(apkversion.getData().get(0).getUrl())
                        .build(getContext())
                        .start();
                AppDialog.INSTANCE.dismissDialog();*/
            }
        });
//取消
        btn_unup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            //因为设置中徐用到,这里加标记
                SpUtils.setParam("localCode",localCode);
                SpUtils.setParam("serverCode",serverCode);
                SpUtils.setParam("up_url",apkversion.getData().get(0).getUrl());
                dialog.dismiss();
            }
        });

    }

//弹框布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/v_u"
        android:layout_width="240dp"
        android:layout_height="300dp"
        android:layout_centerInParent="true"
        android:background="@mipmap/bg_update">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/up_version"
                android:layout_marginTop="75dp"
                android:layout_width="wrap_content"
                android:layout_height="20dp"
                android:text="V1.3.0"
                android:padding="3dp"
                android:layout_marginLeft="80dp"
                android:background="@mipmap/version_button"
                android:textColor="@color/btn_end"
                android:textSize="13sp" />
            <ScrollView
                android:layout_marginTop="15dp"
                android:layout_width="match_parent"
                android:layout_height="75dp">

                <TextView
                    android:id="@+id/up_content"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginLeft="26.5dp"
                    android:layout_marginRight="26.5dp"
                    android:text="更新内容"
                    android:textColor="@color/info_textColor_hint"
                    android:textSize="14sp" />
            </ScrollView>


            <ImageView
                android:layout_marginTop="30dp"
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/mask"
                />
            <ImageView
                android:src="@mipmap/update_button"
                android:id="@+id/btn_up"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:gravity="center"
                android:text="立即更新"
                android:textColor="@color/white"

                android:textSize="15sp" />


        </LinearLayout>

    </RelativeLayout>
    <RelativeLayout
        android:layout_below="@id/v_u"
        android:layout_marginTop="50dp"
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/btn_unup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@mipmap/close_button"/>
    </RelativeLayout>

</RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值