App更新功能

前几天做公司项目的时候需要用到系统自带更新,于是上网看了一些资料,发现一个比较好用的库,今天分享出来。

开源地址:https://github.com/fccaikai/AppUpdate

上自己的效果图:

首先项目依赖:

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

工程依赖:

dependencies {
         compile 'com.github.fccaikai:AppUpdate:2.0.3'
 }

别急,还要写两个类才能使用,这个可以使用库提供的模版:

public class CustomsUpdateActivity extends UpdateActivity {
    @Override
    protected Fragment getUpdateDialogFragment() {
        return CustomsUpdateFragment.newInstance(mModel);
    }
}

XML:

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="52dp"
        android:background="@color/colorPrimary"
        android:elevation="6dp"
        android:gravity="center"
        android:text="发现新版本"
        android:textColor="#ffffff"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:lines="8"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:textColor="#525252"
        android:textSize="18sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="8dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="12dp">

        <Button
            android:id="@+id/cancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:layout_weight="1"
            android:background="@drawable/gray_style"
            android:text="取消"
            android:textColor="#fff"
            android:textSize="18sp" />

        <Button
            android:id="@+id/update"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:layout_weight="1"
            android:background="@drawable/test"
            android:text="升级"
            android:textColor="#fff"
            android:textSize="18sp" />

    </LinearLayout>
</LinearLayout>
public class CustomsUpdateFragment extends UpdateDialog {

    public static CustomsUpdateFragment newInstance(VersionModel model) {
        Bundle args = new Bundle();
        args.putSerializable(Constant.MODEL, model);
        CustomsUpdateFragment fragment = new CustomsUpdateFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_update_dialog;
    }

    @Override
    protected void setContent(View view, int contentId) {
        super.setContent(view, R.id.content);
    }

    @Override
    protected void bindUpdateListener(View view, int updateId) {
        super.bindUpdateListener(view, R.id.update);
    }

    @Override
    protected void bindCancelListener(View view, int cancelId) {
        super.bindCancelListener(view, R.id.cancel);
    }

    @Override
    protected void initIfMustUpdate(View view, int id) {
        super.initIfMustUpdate(view, R.id.cancel);
    }
}

另外还要有一个网络地址,里面是固定的Json格式的数据(如下),会自动解析并设置到弹框的内容上,很方便。

{
  "versionCode":11,
  "versionName":"2.5.0",
  "content":"1.新功能一#2.新功能二",
  "mustUpdate":false,
  "url":"app的下载包的地址"
}

然后就可以自己调用了(个人写的工具类,更新策略为一天只提示一次更新,可根据个人需求自由更改):

public class AppUpdateUtil {
    /**
     * app的更新提示框
     */
    public static void update_app_dialog(Context context) {
        UpdateWrapper updateWrapper = new UpdateWrapper.Builder(context)
                .setTime(100)
                .setNotificationIcon(R.mipmap.ic_launcher)
                .setUrl("https://app.duoduofenqi.com/index.php/Home/AppNotify/getAndroidVersion")
                .setCustomsActivity(CustomsUpdateActivity.class)
                .setCallback(new CheckUpdateTask.Callback() {
                    @Override
                    public void callBack(VersionModel versionModel) {
                        Log.i("update_app_dialog", "版本等于" + versionModel.getVersionCode());
                    }
                })
                .build();
        updateWrapper.start();
    }

    /**
     * app更新弹框提示策略(一天只提示一次)
     */
    public static void update_app(Context context) {
        long l = (Calendar.getInstance().getTimeInMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
        String current_time = sdf.format(new Date(Long.parseLong(String.valueOf(l))));
        Log.i("time", "当前时间戳: " + l);
        Log.i("time", "当前日期: " + current_time);
        String last_update_time = String.valueOf(SPutils.get("last_update_time", ""));
        if (TextUtils.isEmpty(last_update_time)) {
            update_app_dialog(context);
            SPutils.put("last_update_time", current_time);
        } else {
            try {
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                Date current_date = df.parse(current_time);//现在日期
                Date last_date = df.parse(String.valueOf(SPutils.get("last_update_time", "")));//上一次提示更新日期
                int diff_days = (int) ((Math.abs(current_date.getTime() - last_date.getTime())) / (1000 * 60 * 60 * 24));
                if (diff_days >= 1) {
                    update_app_dialog(context);
                    SPutils.put("last_update_time", current_time);
                }
                Log.i("time", "上次更新相差天数:" + diff_days);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
}

 

转载于:https://my.oschina.net/heyongrui/blog/866027

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值