2023年11月26日发(作者:)

Android应⽤的⾃动升级、更新模块的实现完整⽅案+参考程序

我们看到很多Android应⽤都具有⾃动更新功能,⽤户⼀键就可以完成软件的升级更新。得益于Android系统的软件包管理和安装机制,这⼀

功能实现起来相当简单,下⾯我们就来实践⼀下。⾸先给出界⾯效果:

1. 准备知识

⾥定义了每个Android apk的版本标识:

1.

2. package=""

3. android:versionCode="1"

4. android:versionName="1.0.0">

5.

6.

复制代码

其中,android:versionCodeandroid:versionName两个字段分别表⽰版本代码,版本名称。versionCode是整型数字,versionName是字

符串。由于version是给⽤户看的,不太容易⽐较⼤⼩,升级检查时,可以以检查versionCode为主,⽅便⽐较出版本的前后⼤⼩。

那么,在应⽤中如何读取中的versionCodeversionName呢?可以使⽤PackageManagerAPI,参考以下代码:

1. public static int getVerCode(Context context) {

2. int verCode = -1;

3. try {

4. verCode = kageManager().getPackageInfo(

5. "", 0).versionCode;

6. } catch (NameNotFoundException e) {

7. Log.e(TAG, sage());

8. }

9. return verCode;

10. }

11.

12. public static String getVerName(Context context) {

13. String verName = "";

14. try {

15. verName = kageManager().getPackageInfo(

16. "", 0).versionName;

17. } catch (NameNotFoundException e) {

18. Log.e(TAG, sage());

19. }

20. return verName;

21. }

复制代码

或者在AndroidManifest中将android:versionName="1.2.0"写成android:versionName="@string/app_versionName",然后在

values/中添加对应字符串,这样实现之后,就可以使⽤如下代码获得版本名称:

1. public static String getVerName(Context context) {

2. String verName = ources()

3. .getText(_versionName).toString();

4. return verName;

5. }

复制代码

同理,apk的应⽤名称可以这样获得:

1. public static String getAppName(Context context) {

2. 流程框架

1. [{"appname":"jtapp12","apkname":"","verName":1.0.1,"verCode":2}]

复制代码

然后,在⼿机客户端上进⾏版本读取和检查:

1. private boolean getServerVer () {

2. try {

3. String verjson = tent(_SERVER

4. + _VERJSON);

5. JSONArray array = new JSONArray(verjson);

6. if (() > 0) {

7. JSONObject obj = NObject(0);

8. try {

9. newVerCode = nt(ing("verCode"));

10. newVerName = ing("verName");

11. } catch (Exception e) {

12. newVerCode = -1;

13. newVerName = "";

14. return false;

15. }

15. public void onClick(DialogInterface dialog,

16. int which) {

17. finish();

18. }

19. }).create();// 创建

20. // 显⽰对话框

21. ();

22. }

23. private void doNewVersionUpdate() {

24. int verCode = Code(this);

25. String verName = Name(this);

26. StringBuffer sb = new StringBuffer();

27. ("当前版本:");

28. (verName);

29. (" Code:");

30. (verCode);

31. (", 发现新版本:");

32. (newVerName);

33. (" Code:");

34. (newVerCode);

35. (", 是否更新?");

36. Dialog dialog = new r()

37. .setTitle("软件更新")

38. .setMessage(ng())

39. // 设置内容

40. .setPositiveButton("更新",// 设置确定按钮

41. new kListener() {

42. @Override

15. File file = new File(

16. ernalStorageDirectory(),

17. _SAVENAME);

18. fileOutputStream = new FileOutputStream(file);

19. byte[] buf = new byte[1024];

20. int ch = -1;

21. int count = 0;

22. while ((ch = (buf)) != -1) {

23. (buf, 0, ch);

24. count += ch;

25. if (length > 0) {

26. }

27. }

28. }

29. ();

30. if (fileOutputStream != null) {

31. ();

32. }

33. down();

34. } catch (ClientProtocolException e) {

35. tackTrace();

36. } catch (IOException e) {

37. tackTrace();

38. }