在app开发中怎么实现app打开自动更新

在app开发中怎么实现app打开自动更新,第1张

Android开发如何实现APP自动更新                             

     

先来看看要实现的效果图:

对于安卓用户来说,手机应用市场说满天飞可是一点都不夸张,比如小米,魅族,百度,360,机锋,应用宝等等,当我们想上线一款新版本APP时,先不说渠道打包的麻烦,单纯指上传APP到各大应用市场的工作量就已经很大了,好不容易我们把APP都上传完了,突然发现一个会导致应用闪退的小Bug,这时那个崩溃啊,明明不是很大的改动,难道我们还要再去重新去把各大应用市场的版本再上传更新一次?相信我,运营人员肯定会弄死你的!!

有问题,自然就会有解决问题的方案,因此我们就会想到如果在APP里内嵌自动更新的功能,那么我们将可以省去很多麻烦,当然关于这方面功能的第三方SDK有很多。

好了,言归正传,今天我们自己来实现下关于APP自动更新。

流程其实并不复杂:当用户打开APP的时候,我们让APP去发送一个检查版本的网络请求,或者利用服务端向APP推送一个透传消息来检查APP的版本,如果当前APP版本比服务器上的旧,那么我们就提醒用户进行下载更新APP,当然在特定的情况下,我们也可以强制的让用户去升级,当然这是很不友好的,尽可能的减少这样的做法。

好了,来梳理下流程,首先既然是一个APP的更新,那么我们就需要去下载新的APP,然后我们需要一个通知来告诉用户当前的下载进度,再来当APP安装包下载完成后,我们需要去系统的安装程序来对APP进行安装更新。

知识点:

下载:异步HTTP请求文件下载,并监听当前下载进度(这里我采用了okhttp)

通知:Notification(具体用法请自行翻阅API文档)

安装:Intent (具体用法请自行翻阅API文档)

来看下具体实现代码:

我们需要一个后台服务来支撑App的下载

import android.app.Notification  

import android.app.NotificationManager  

import android.app.PendingIntent  

import android.app.Service  

import android.content.Intent  

import android.graphics.BitmapFactory  

import android.net.Uri  

import android.os.IBinder  

import android.support.annotation.Nullable  

import android.support.v7.app.NotificationCompat  

   

import com.fangku.commonlibrary.utils.StorageUtil  

import com.zhy.http.okhttp.OkHttpUtils  

import com.zhy.http.okhttp.callback.FileCallBack  

   

import java.io.File  

   

import okhttp3.Call  

   

/** 

 * 自动下载更新apk服务 

 * Create by: chenwei.li 

 * Date: 2016-08-14 

 * time: 09:50 

 * Email: lichenwei.me@foxmail.com 

 */  

public class DownloadService extends Service {  

   

 private String mDownloadUrl//APK的下载路径  

 private NotificationManager mNotificationManager  

 private Notification mNotification  

   

   

 @Override  

 public void onCreate() {  

 super.onCreate()  

 mNotificationManager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE)  

   

 }  

   

 @Override  

 public int onStartCommand(Intent intent, int flags, int startId) {  

 if (intent == null) {  

  notifyMsg("温馨提醒", "文件下载失败", 0)  

  stopSelf()  

 }  

 mDownloadUrl = intent.getStringExtra("apkUrl")//获取下载APK的链接  

 downloadFile(mDownloadUrl)//下载APK  

 return super.onStartCommand(intent, flags, startId)  

 }  

   

 @Nullable  

 @Override  

 public IBinder onBind(Intent intent) {  

 return null  

 }  

   

 private void notifyMsg(String title, String content, int progress) {  

   

 NotificationCompat.Builder builder = new NotificationCompat.Builder(this)//为了向下兼容,这里采用了v7包下的NotificationCompat来构造  

 builder.setSmallIcon(R.mipmap.icon_login_logo).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.icon_login_logo)).setContentTitle(title)  

 if (progress > 0 && progress < 100) {  

  //下载进行中  

  builder.setProgress(100, progress, false)  

 } else {  

  builder.setProgress(0, 0, false)  

 }  

 builder.setAutoCancel(true)  

 builder.setWhen(System.currentTimeMillis())  

 builder.setContentText(content)  

 if (progress >= 100) {  

  //下载完成  

  builder.setContentIntent(getInstallIntent())  

 }  

 mNotification = builder.build()  

 mNotificationManager.notify(0, mNotification)  

   

   

 }  

   

 /** 

 * 安装apk文件 

 * 

 * @return 

 */  

 private PendingIntent getInstallIntent() {  

 File file = new File(StorageUtil.DOWNLOAD_DIR + "APP文件名")  

 Intent intent = new Intent(Intent.ACTION_VIEW)  

 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)  

 intent.setDataAndType(Uri.parse("file://" + file.getAbsolutePath()), "application/vnd.android.package-archive")  

 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)  

 return pendingIntent  

 }  

   

   

 /** 

 * 下载apk文件 

 * 

 * @param url 

 */  

 private void downloadFile(String url) {  

   

 OkHttpUtils.get().url(url).build().execute(new FileCallBack(StorageUtil.DOWNLOAD_DIR, "APP文件名") {  

  @Override  

  public void onError(Call call, Exception e, int id) {  

  notifyMsg("温馨提醒", "文件下载失败", 0)  

  stopSelf()  

  }  

   

  @Override  

  public void onResponse(File response, int id) {  

  //当文件下载完成后回调  

  notifyMsg("温馨提醒", "文件下载已完成", 100)  

  stopSelf()  

   

   

  }  

   

  @Override  

  public void inProgress(float progress, long total, int id) {  

  //progress*100为当前文件下载进度,total为文件大小  

  if ((int) (progress * 100) % 10 == 0) {  

   //避免频繁刷新View,这里设置每下载10%提醒更新一次进度  

   notifyMsg("温馨提醒", "文件正在下载..", (int) (progress * 100))  

  }  

  }  

 })  

 }  

}

然后我们只需要在我们想要的更新APP的时候去调起这个服务即可,比如在系统设置里的"版本检查"等

Intent intent = new Intent(mContext, DownloadService.class)  

intent.putExtra("apkUrl", "APK下载地址")  

startService(intent)

总结

这里我只是粗略演示本地自动更新APP的功能,在实际应用中,我们应该配合服务端来做,比如在用户启动APP的时候去比对版本号,如果版本号低于服务器的版本号,那么此时服务端应该给客户端一个透传推送,这里的推送内容应该为新版本APP的下载地址,此时就可以根据该地址来下载新版APP了,当遇到重大更新,不再对老版本进行兼容的时候,可以强制用户升级,这里的方案有很多,比如调用系统级对话框,让用户没办法取消等 *** 作,这里就不做更多描述。以上就是这篇文章的全部内容,希望对有需要的人能有所帮助。

android 实现应用内更新的方法为:

1、新建一个Android工程命名为:UpdateDemo。

2、新建一个UpdateManager.java类,负责软件更新功能模块,代码如下:

package com.tutor.update

import java.io.File

import java.io.FileOutputStream

import java.io.IOException

import java.io.InputStream

import java.net.HttpURLConnection

import java.net.MalformedURLException

import java.net.URL

import android.app.AlertDialog

import android.app.Dialog

import android.app.AlertDialog.Builder

import android.content.Context

import android.content.DialogInterface

import android.content.Intent

import android.content.DialogInterface.OnClickListener

import android.net.Uri

import android.os.Handler

import android.os.Message

import android.view.LayoutInflater

import android.view.View

import android.widget.ProgressBar

public class UpdateManager {

private Context mContext

//提示语

private String updateMsg = "有最新的软件包哦,亲快下载吧~"

//返回的安装包url

private String apkUrl = "http://softfile.3g.qq.com:8080/msoft/179/24659/43549/qq_hd_mini_1.4.apk"

private Dialog noticeDialog

private Dialog downloadDialog

/* 下载包安装路径 */

private static final String savePath = "/sdcard/updatedemo/"

private static final String saveFileName = savePath + "UpdateDemoRelease.apk"

/* 进度条与通知ui刷新的handler和msg常量 */

private ProgressBar mProgress

private static final int DOWN_UPDATE = 1

private static final int DOWN_OVER = 2

private int progress

private Thread downLoadThread

private boolean interceptFlag = false

private Handler mHandler = new Handler(){

public void handleMessage(Message msg) {

switch (msg.what) {

case DOWN_UPDATE:

mProgress.setProgress(progress)

break

case DOWN_OVER:

installApk()

break

default:

break

}

}

}

public UpdateManager(Context context) {

this.mContext = context

}

//外部接口让主Activity调用

public void checkUpdateInfo(){

showNoticeDialog()

}

private void showNoticeDialog(){

AlertDialog.Builder builder = new Builder(mContext)

builder.setTitle("软件版本更新")

builder.setMessage(updateMsg)

builder.setPositiveButton("下载", new OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

dialog.dismiss()

showDownloadDialog()

}

})

builder.setNegativeButton("以后再说", new OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

dialog.dismiss()

}

})

noticeDialog = builder.create()

noticeDialog.show()

}

private void showDownloadDialog(){

AlertDialog.Builder builder = new Builder(mContext)

builder.setTitle("软件版本更新")

final LayoutInflater inflater = LayoutInflater.from(mContext)

View v = inflater.inflate(R.layout.progress, null)

mProgress = (ProgressBar)v.findViewById(R.id.progress)

builder.setView(v)

builder.setNegativeButton("取消", new OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

dialog.dismiss()

interceptFlag = true

}

})

downloadDialog = builder.create()

downloadDialog.show()

downloadApk()

}

private Runnable mdownApkRunnable = new Runnable() {

@Override

public void run() {

try {

URL url = new URL(apkUrl)

HttpURLConnection conn = (HttpURLConnection)url.openConnection()

conn.connect()

int length = conn.getContentLength()

InputStream is = conn.getInputStream()

File file = new File(savePath)

if(!file.exists()){

file.mkdir()

}

String apkFile = saveFileName

File ApkFile = new File(apkFile)

FileOutputStream fos = new FileOutputStream(ApkFile)

int count = 0

byte buf[] = new byte[1024]

do{

int numread = is.read(buf)

count += numread

progress =(int)(((float)count / length) * 100)

//更新进度

mHandler.sendEmptyMessage(DOWN_UPDATE)

if(numread <= 0){

//下载完成通知安装

mHandler.sendEmptyMessage(DOWN_OVER)

break

}

fos.write(buf,0,numread)

}while(!interceptFlag)//点击取消就停止下载.

fos.close()

is.close()

} catch (MalformedURLException e) {

e.printStackTrace()

} catch(IOException e){

e.printStackTrace()

}

}

}

/**

* 下载apk

* @param url

*/

private void downloadApk(){

downLoadThread = new Thread(mdownApkRunnable)

downLoadThread.start()

}

/**

* 安装apk

* @param url

*/

private void installApk(){

File apkfile = new File(saveFileName)

if (!apkfile.exists()) {

return

}

Intent i = new Intent(Intent.ACTION_VIEW)

i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive")

mContext.startActivity(i)

}

}

第三步:在MainActivity.java也就是主Activity调用,代码如下:

package com.tutor.update

import android.app.Activity

import android.os.Bundle

public class MainAcitivity extends Activity {

private UpdateManager mUpdateManager

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState)

setContentView(R.layout.main)

//这里来检测版本是否需要更新

mUpdateManager = new UpdateManager(this)

mUpdateManager.checkUpdateInfo()

}

}

3、添加程序所用的资源与权限:

下载的时候用到了ProgressBar,所以事先写了一个progress.xml布局文件,代码如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="wrap_content">

<ProgressBar

android:id="@+id/progress"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

style="?android:attr/progressBarStyleHorizontal"

/>

</LinearLayout>

4、运行查看。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11939783.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-19
下一篇2023-05-19

发表评论

登录后才能评论

评论列表(0条)

    保存