之前有篇博文是使用第三方蒲公英提示更新,但是蒲公英更新推荐大家内测使用,实际的开发中我们还得需要自己来编写版本更新代码,我就简单了写了一个简单demo,大致的功能就是如果有新的版本,提示更新下载到sd卡位置 不多说怼代码:
代码中用到了okhttp框架相信大家都会导入就不在这写了.
demo目录(Android studio): 效果图:
json数据:(versionName版本号,url是apk下载的地址,title是更新的内容)
{
"versionName": "1.02",
"versionCode": 1,
"url": "http://100.100.110.1/IXCd8a8c6a3058ab4c74b4e3af99c6c2fb7/data/wisegame/
09bd127e31d541a2/weibo_3424.apk",
"title": "1、新版本提示
2、换行测试
2、换行测试"
}
1、activity_main布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.wdl.updatedemo.MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="检查新版本" />
</RelativeLayout>
2、创建dialog.xml布局,显示弹框
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical"
>
<TextView
android:layout_margin="10dp"
android:id="@+id/text_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="更新内容"
android:textSize="18sp" />
<TextView
android:layout_marginLeft="10dp"
android:layou