背景
本项目的存在是因为友盟关闭了自动更新服务,所以自己结合友盟所说的一些自动更新方案,利用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();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
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);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
AndroidManifest.xml配置
<service android:name="com.trycath.myupdateapklibrary.service.DownloadFileService" />