利用fir.im提供的api接口实现android apk自动更新和手动更新

时间:2021-12-07 06:01:46

github地址

背景

本项目的存在是因为友盟关闭了自动更新服务,所以自己结合友盟所说的一些自动更新方案,利用Rxjava + Retrofit2 + Okhttp3 + Rxandroid + Service + Fim实现app自动更新

使用范围

适用于api 14及以上,且加入了Android6.0的权限判断,由于本人很懒就没开发后台apk管理功能,就直接采用fim的公共api实现版本比对

Gradle Config

 dependencies {
compile 'com.trycatch.android:myupdateapklibrary:1.2.6'
}

使用

首先必须保证你的最新版本versionCode和versionName是随版本升级递增的
1.自动更新 Activity

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UpdateKey.API_TOKEN = "160105e0223dec1896a6648b8265cc95";
UpdateKey.APP_ID = "57d75987ca87a87c89000b91";
UpdateKey.WITH_DIALOG=true;
UpdateKey.WITH_NOTIFITION=true;
UpdateApk.init(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
UpdateApk.destory();
}
}

2.手动更新

UpdateApk.setAppUpdateListener(new AppUpdateListener() {
@Override
public void onStart() {
if(dialog!=null&&!dialog.isShowing()){
dialog.show();
}
}

@Override
public void onCompleted() {
Log.d(TAG,"onCompleted()===");
if(dialog!=null&&dialog.isShowing()){
dialog.dismiss();
}
}

@Override
public void onError(Throwable e) {
Log.d(TAG,e.toString());
if(dialog!=null&&dialog.isShowing()){
dialog.dismiss();
}
}

@Override
public void onNext(AppInfoModel appInfoModel) {
Log.d(TAG,appInfoModel.toString());
if(dialog!=null&&dialog.isShowing()){
dialog.dismiss();
}
}

@Override
public void onNext(AppInfoModel appInfoModel, int state) {
Log.d(TAG,appInfoModel.toString());
Log.d(TAG,"=state=="+state);
switch (state){
case UpdateState.BEST_NEW_VSERSION:
Toast.makeText(MainActivity.this,"this is best new version",Toast.LENGTH_SHORT).show();
break;
case UpdateState.BEST_HEIGHT_VERSION:
Toast.makeText(MainActivity.this,"this is highest version",Toast.LENGTH_SHORT).show();
break;
case UpdateState.NEED_UPDATE_VERSION:
Toast.makeText(MainActivity.this,"need update new version",Toast.LENGTH_SHORT).show();
break;
default:

}
if(dialog!=null&&dialog.isShowing()){
dialog.dismiss();
}
}
});
UpdateApk.init(MainActivity.this);

AndroidManifest.xml配置

<service android:name="com.trycath.myupdateapklibrary.service.DownloadFileService" />