V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ck19920702
V2EX  ›  Android

Android App 检查更新库

  •  
  •   ck19920702 · 2017-07-07 13:27:10 +08:00 · 5397 次点击
    这是一个创建于 2485 天前的主题,其中的信息可能已经有所发展或是发生改变。

    AppUpdate

    Android 检查更新库

    屏幕截图

    如何使用

    配置

    • Step 1.配置 jitpack 仓库

      在根目录的gradle.build中添加 jitpack 地址:

      allprojects {
      	repositories {
      		...
      		maven { url 'https://jitpack.io' }
      	}
      }
      
    • Step 2. 添加库依赖:

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

    使用

    UpdateWrapper updateWrapper = new UpdateWrapper.Builder(getApplicationContext())
        					//set interval Time
        	                .setTime(time)
        	                //set notification icon
        	                .setNotificationIcon(R.mipmap.ic_launcher_round)
        	                //set update file url
        	                .setUrl("you update json file url").build();
    updateWrapper.start();
    

    升级 json 文件格式:

    {
      "versionCode":1,
      "versionName":"1.0.0",
      "content":"1.新增抢单功能#2.性能优化",//使用 # 来进行换行
      "mustUpdate":true,	//是否强制 ,true or false
      "url":"apk download url"
    }
    

    自定义 UI

    • 自定义 Activity 创建一个 Activity 继承自UpdateActivity,并重写protected Fragment getUpdateDialogFragment()方法,比如:
    public class CustomsUpdateActivity extends UpdateActivity {
        @Override
        protected Fragment getUpdateDialogFragment() {
            return CustomsUpdateFragment.newInstance(mModel);
        }
    }
    
    • 设置主题

      Activity 需要设置为 Dialog 主题,在Androidmanifest.xml中注册 Activity 中设置主题

      <activity 
                android:name=".CustomsUpdateActivity"
                android:theme="@style/UpdateDialog">
      </activity>
      

    • 自定义 FragmentDialog

    创建一个 FragmentDialog,继承自UpdateDialog,代码如下:

    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);
        }
    }
    
    • 配置
     UpdateWrapper.Builder builder = ...;
     builder.setCustomsActivity(CustomsUpdateActivity.class);
     ...
     builder.build().start();
    

    具体使用请查看demo

    依赖的库

    • v7-support

    compile 'com.android.support:appcompat-v7:25.2.0'

    如果你不想使用这两个库,可以使用exclude排除

    源码地址:AppUpdate

    8 条回复    2017-07-07 14:47:38 +08:00
    choury
        1
    choury  
       2017-07-07 13:30:05 +08:00   ❤️ 1
    应用内更新,深恶痛绝
    qq1455018613
        2
    qq1455018613  
       2017-07-07 14:01:17 +08:00
    应用内更新
    also24
        3
    also24  
       2017-07-07 14:06:03 +08:00
    @choury #1
    @qq1455018613 #2

    如果用户是从官网下载的 APP,请问不通过这种方式的话,需要用什么方法来提醒用户更新新版?
    zysidea
        4
    zysidea  
       2017-07-07 14:14:05 +08:00 via Android
    @also24
    一般官方网站 直接贴 Google play 下载链接
    ck19920702
        5
    ck19920702  
    OP
       2017-07-07 14:25:16 +08:00
    @choury 有些企业的 app 可能是不上架市场的,就需要用到这种方式,上架的 App 最好还是通过市场去更新。
    030
        6
    030  
       2017-07-07 14:30:38 +08:00
    @ck19920702 所以不上 google play 为什么要装?(自己 build 的除外
    choury
        7
    choury  
       2017-07-07 14:33:34 +08:00 via Android
    @also24 我为什么要从官网下?有什么好处?不受监管?
    shoaly
        8
    shoaly  
       2017-07-07 14:47:38 +08:00
    有一个已经有的轮子了 前两天还在用
    anyversion
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2828 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 11:45 · PVG 19:45 · LAX 04:45 · JFK 07:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.