首页 > 其他分享 >Android学 App自动更新之通知栏下…

Android学 App自动更新之通知栏下…

时间:2023-07-31 17:07:25浏览次数:33  
标签:栏下 int updateNotification 自动更新 new Android public 下载


Android学习系列(2)--App自动更新之通知栏下载



见证过博客园的多次升级,你也希望你的软件通过更新发布新特性通知用户吧,是的。
这篇文章是android开发人员的必备知识,是我特别为大家整理和总结的,不求完美,但是有用。 

1.设计思路,使用VersionCode定义为版本升级参数。
  android为我们定义版本提供了2个属性:



<manifest package="com.cnblogs.tianxia.subway" android:versionCode="1" <!--Integer类型,系统不显示给用户--> android:versionName="1.0"<!--String类型,系统显示用户--> ></manifest>



   

2.工程目录

  为了对真实项目或者企业运用有实战指导作用,我模拟一个独立的项目,工程目录设置的合理严谨一些,而不是仅仅一个demo。

  假设我们以上海地铁为项目,命名为"Subway",工程结构如下,

      

Android学 <wbr>App自动更新之通知栏下载

3.版本初始化和版本号的对比。
  首先定义在全局文件Global.java中定义变量localVersion和serverVersion分别存放本地版本号和服务器版本号。



public class Global { //版本信息 public static int localVersion = 0; public static int serverVersion = 0; }

 

   因为本文只是重点说明升级更新,为了防止其他太多无关代码冗余其中,我直接在SubwayApplication中定义方法initGlobal()方法。

public void initGlobal(){ try{ Global.localVersion = getPackageManager().getPackageInfo(getPackageName(),0).versionCode; //设置本地版本号 Global.serverVersion = 1;//假定服务器版本为2,本地版本默认是1 }catch (Exception ex){ ex.printStackTrace(); } }

 

      如果检测到新版本发布,提示用户是否更新,我在SubwayActivity中定义了checkVersion()方法:

public void checkVersion(){ if(Global.localVersion < Global.serverVersion){ //发现新版本,提示用户更新 AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setTitle("软件升级") .setMessage("发现新版本,建议立即更新使用.") .setPositiveButton("更新", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //开启更新服务UpdateService //这里为了把update更好模块化,可以传一些updateService依赖的值 //如布局ID,资源ID,动态获取的标题,这里以app_name为例 Intent updateIntent =new Intent(SubwayActivity.this, UpdateService.class); updateIntent.putExtra("titleId",R.string.app_name); startService(updateIntent); } }) .setNegativeButton("取消",new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); alert.create().show(); }else{ //清理工作,略去 //cheanUpdateFile(),文章后面我会附上代码 } }

如下图:


Android学 <wbr>App自动更新之通知栏下载


  好,我们现在把这些东西串一下:


  第一步

在SubwayApplication的onCreate()方法中执行initGlobal()初始化版本变量


public void onCreate() { super.onCreate(); initGlobal(); }

 

      第二步 在SubwayActivity的onCreate()方法中检测版本更新checkVersion()。


public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); checkVersion(); }



  现在入口已经打开,在checkVersion方法的第18行代码中看出,当用户点击更新,我们开启更新服务,从服务器上下载最新版本。

4.使用Service在后台从服务器端下载,完成后提示用户下载完成,并关闭服务。
  定义一个服务UpdateService.java,首先定义与下载和通知相关的变量:



//标题 private int titleId = 0; //文件存储 private File updateDir = null;   private File updateFile = null; //通知栏 private NotificationManager updateNotificationManager = null; private Notification updateNotification = null; //通知栏跳转Intent private Intent updateIntent = null; private PendingIntent updatePendingIntent = null;

在onStartCommand()方法中准备相关的下载工作:


@Override public int onStartCommand(Intent intent, int flags, int startId) { //获取传值 titleId = intent.getIntExtra("titleId",0); //创建文件 if(android.os.Environment.MEDIA_MOUNTED.equals(android.os.Environment.getExternalStorageState())){ updateDir = new File(Environment.getExternalStorageDirectory(),Global.downloadDir); updateFile = new File(updateDir.getPath(),getResources().getString(titleId)+".apk"); } this.updateNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); this.updateNotification = new Notification(); //设置下载过程中,点击通知栏,回到主界面 updateIntent = new Intent(this, SubwayActivity.class); updatePendingIntent = PendingIntent.getActivity(this,0,updateIntent,0); //设置通知栏显示内容 updateNotification.icon = R.drawable.arrow_down_float; updateNotification.tickerText = "开始下载"; updateNotification.setLatestEventInfo(this,"上海地铁","0%",updatePendingIntent); //发出通知 updateNotificationManager.notify(0,updateNotification); //开启一个新的线程下载,如果使用Service同步下载,会导致ANR问题,Service本身也会阻塞 new Thread(new updateRunnable()).start();//这个是下载的重点,是下载的过程 return super.onStartCommand(intent, flags, startId); }

上面都是准备工作,如图:


Android学 <wbr>App自动更新之通知栏下载


  从代码中可以看出来,updateRunnable类才是真正下载的类,出于用户体验的考虑,这个类是我们单独一个线程后台去执行的。


  下载的过程有两个工作:1.从服务器上下载数据;2.通知用户下载的进度。


  线程通知,我们先定义一个空的updateHandler。


private Handler updateHandler = new Handler(){ @Override public void handleMessage(Message msg) { } };

 

      再来创建updateRunnable类的真正实现:

class updateRunnable implements Runnable { Message message = updateHandler.obtainMessage(); public void run() { message.what = DOWNLOAD_COMPLETE; try{ //增加权限; if(!updateDir.exists()){ updateDir.mkdirs(); } if(!updateFile.exists()){ updateFile.createNewFile(); } //下载函数,以QQ为例子 //增加权限; long downloadSize = downloadUpdateFile("http://softfile.3g.qq.com:8080/msoft/179/1105/10753/MobileQQ1.0(Android)_Build0198.apk",updateFile); if(downloadSize>0){ //下载成功 updateHandler.sendMessage(message); } }catch(Exception ex){ ex.printStackTrace(); message.what = DOWNLOAD_FAIL; //下载失败 updateHandler.sendMessage(message); } } }

下载函数的实现有很多,我这里把代码贴出来,而且我们要在下载的时候通知用户下载进度:


public long downloadUpdateFile(String downloadUrl, File saveFile) throws Exception { //这样的下载代码很多,我就不做过多的说明 int downloadCount = 0; int currentSize = 0; long totalSize = 0; int updateTotalSize = 0; HttpURLConnection httpConnection = null; InputStream is = null; FileOutputStream fos = null; try { URL url = new URL(downloadUrl); httpConnection = (HttpURLConnection)url.openConnection(); httpConnection.setRequestProperty("User-Agent", "PacificHttpClient"); if(currentSize > 0) { httpConnection.setRequestProperty("RANGE", "bytes=" + currentSize + "-"); } httpConnection.setConnectTimeout(10000); httpConnection.setReadTimeout(20000); updateTotalSize = httpConnection.getContentLength(); if (httpConnection.getResponseCode() == 404) { throw new Exception("fail!"); } is = httpConnection.getInputStream(); fos = new FileOutputStream(saveFile, false); byte buffer[] = new byte[4096]; int readsize = 0; while((readsize = is.read(buffer)) > 0){ fos.write(buffer, 0, readsize); totalSize += readsize; //为了防止频繁的通知导致应用吃紧,百分比增加10才通知一次 if((downloadCount == 0)||(int) (totalSize*100/updateTotalSize)-10>downloadCount){ downloadCount += 10; updateNotification.setLatestEventInfo(UpdateService.this, "正在下载", (int)totalSize*100/updateTotalSize+"%", updatePendingIntent); updateNotificationManager.notify(0, updateNotification); } } } finally { if(httpConnection != null) { httpConnection.disconnect(); } if(is != null) { is.close(); } if(fos != null) { fos.close(); } } return totalSize; }

显示下载进度,如图:


Android学 <wbr>App自动更新之通知栏下载


下载完成后,我们提示用户下载完成,并且可以点击安装,那么我们来补全前面的Handler吧。


先在UpdateService.java定义2个常量来表示下载状态:


//下载状态 private final static int DOWNLOAD_COMPLETE = 0; private final static int DOWNLOAD_FAIL = 1;

根据下载状态处理主线程:


private Handler updateHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch(msg.what){ case DOWNLOAD_COMPLETE: //点击安装PendingIntent Uri uri = Uri.fromFile(updateFile); Intent installIntent = new Intent(Intent.ACTION_VIEW); installIntent.setDataAndType(uri, "application/vnd.android.package-archive"); updatePendingIntent = PendingIntent.getActivity(UpdateService.this, 0, installIntent, 0); updateNotification.defaults = Notification.DEFAULT_SOUND;//铃声提醒 updateNotification.setLatestEventInfo(UpdateService.this, "上海地铁", "下载完成,点击安装。", updatePendingIntent); updateNotificationManager.notify(0, updateNotification); //停止服务 stopService(updateIntent); case DOWNLOAD_FAIL: //下载失败 updateNotification.setLatestEventInfo(UpdateService.this, "上海地铁", "下载完成,点击安装。", updatePendingIntent); updateNotificationManager.notify(0, updateNotification); default: stopService(updateIntent); } } };


下载完成,如图:

Android学 <wbr>App自动更新之通知栏下载


至此,文件下载并且在通知栏通知进度。

发现本人废话很多,其实几句话的事情,来来回回写了这么多,啰嗦了,后面博文我会朝着精简方面努力。

PS:前面说要附上cheanUpdateFile()的代码

File updateFile = new File(Global.downloadDir,getResources().getString(R.string.app_name)+".apk");if(updateFile.exists()){ //当不需要的时候,清除之前的下载文件,避免浪费用户空间 updateFile.delete(); }

谢谢大家!!!!


标签:栏下,int,updateNotification,自动更新,new,Android,public,下载
From: https://blog.51cto.com/u_3124497/6910104

相关文章

  • Android中dispatchTouchEvent,&nbs…
    onInterceptTouchEvent用于改变事件的传递方向。决定传递方向的是返回值,返回为false时事件会传递给子控件,返回值为true时事件会传递给当前控件的onTouchEvent(),这就是所谓的Intercept(拦截)。[tisaps:正确的使用方法是,在此方法内仅判断事件是否需要拦截,然后返回。即便需要拦截......
  • Android应用如何适配不同分辨率的…
           Android应用如何适配不同分辨率的手机主要分三块考虑1)界面配置根据不同的分辨率,创建手机界面文件例子:在res下创建layout-800x480    ......
  • Android开发FAQ-ContentObserver应…
    一、需求1.产品不同界面显示数据个数.2.数据个数动态改变.3.涉及数据与本地数据库有关可采用ContentObserver技术实现二、代码实现如下:mCursorObserver=newContentObserver(mHandler){          @Override          publicvoidonChange(booleanse......
  • Android 设计模式:(二)观察者模…
    Android设计模式:(二)观察者模式——让你的对象知悉现况设计模式2012-05-2813:28 1074人阅读 评论(1) 收藏 举报*观察者模式:定义了对象之间的一对多依赖关系,当一个对象(主题对象)的状态改变时,它的所有依赖者(观察者对象)都会收到通知并自动更新。*观察......
  • Android NFC Mifare Tag 读写示例
    前面例子介绍了检测,读写NFCTAG开发的一般步骤,本例针对常用的MifareTag具体说明。MifareTag可以有1K,2K,4K,其内存分区大同小异,下图给出了1K字节容量的Tag的内存分布:数据分为16个区(Sector),每个区有4个块(Block),每个块可以存放16字节的数据,其大小为16X4X16=1024......
  • 关于Android流畅度不如iOS的几点看…
    关于Android流畅度不如iOS的几点看法网上一名据称是前谷歌实习生的人透露了一些关于Android系统硬件加速的内幕。据称,在Android3.0和4.0之前,并没有完整的硬件加速。他们一直在通过硬件加速绘制某些UI元素,并称效果并不像他们想象中那样乐观。他认为,Android效率低下的设计框......
  • Android应用开发的插件化 模块化
    在android的项目开发中,都会遇到后期功能拓展增强与主程序代码变更的现实矛盾,也就是程序的灵活度。  由于linux平台的安全机制,再加上dalvik的特殊机制,各种权限壁垒,使得开发一个灵活多变的程序,变得比较困难,不像pc平台下那么容易。  瞅瞅elipse的插件,瞅瞅360的插件,在an......
  • Android解耦(四)基于依赖注入的解耦
    安卓开发中基于依赖注入(DI)的模块解耦1.什么是依赖注入(DI)依赖注入(DependencyInjection,简称DI)是一种设计模式,用于实现控制反转(InversionofControl,简称IoC)。控制反转是指将对象之间的依赖关系由程序代码中定义转移到外部容器中管理,从而降低对象之间的耦合度,提高代码的可维护性和......
  • JDK 版本异常导致 flutter doctor --android-licenses 出错 (class file version 61.0
    flutterdoctor--android-licensesError:AJNIerrorhasoccurred,pleasecheckyourinstallationandtryagainExceptioninthread"main"java.lang.UnsupportedClassVersionError:com/android/sdklib/tool/sdkmanager/SdkManagerClihasbeencompil......
  • Android系统原理
    Android的系统架构可以分为四个层次:应用层、应用框架层、系统运行库层和Linux内核层。应用层 是直接面向用户的,包括各种应用程序和用户界面。应用框架层提供了各种API和服务,使得应用程序可以访问系统资源和功能。系统运行库层 提供各种系统库,包括C库、媒体库、图......