Service作为Android四大组件之一,使用的场景是极为广泛的。日前在开发过程中,就被要求要做个应用内升级的功能。我的第一反应就是用service来实现。
先来说一下我们的思路:
1.通过后台接口返回的数据,判断是否需要进行新版本apk的下载
2.当需要更新时,我们要给出一个dialog来提示用户升级。
3.如果用户确认升级,启动servce,在后台进行apk文件的下载
4.下载完成之后,跳转到APK的安装页面。
先看第一步,各个公司后台返回的数据其实是不一样的,但是都会有版本号,版本名称,apk下载地址这几个参数。
我们需要做的就是将参数与当前apk的版本号进行对比。
// 获取当前APK的版本号
PackageManager manager = getPackageManager();
PackageInfo packageInfo = manager.getPackageInfo(getPackageName(), 0);
int versionCode = packageInfo.versionCode;
LogUtil.LogI("获取当前APK版本号:" + versionCode);
我们获取到的版本versioncode即为我们gradle文件里的versionCode。
第二步,给用户的dialog可以用到我们上一篇中使用的dialogfragment来做,传送门。在此不做详细叙述。
dialog里面只需要放置一个button,点击事件为开启服务,就会满足我们的需求,当然实际开发中会给用户展示出新版本的特性等等。
第三步,开启service,并下载apk。
我们应该都知道,开启服务用两种方式,startservice和bindservice。这两者的区别就在于各自的生命周期,startservice时,service可以独立存在,不需要与开启的对象有任何交互,而安装APK 这个操作恰恰符合这一特性,所以我们用startservice来启动service。
Intent it = new Intent(getActivity(), DownLoadService.class);
it.putExtra("apkUrl",apkUrl);
getActivity().startService(it);
先创建一个DownloadService,继承自service。
public class DownLoadService extends Service {
private String apkPath;
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
if (!TextUtils.isEmpty(apkPath)){
installAPK();
}
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
LogUtil.LogI("onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle bundle = intent.getExtras();
String apkUrl = bundle.getString("apkUrl");
downloadAPK(apkUrl);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
可以看到,我们重写了service的onBind,onCreat,onStartCommad,onDestroy这几个方法,这也是service的重要的几个生命周期方法。
在onStartCommand方法中,我们接到从dialog中传入的apk下载地址,由此开始下载apk。
/**
* apk下载
* @param apkUrl
*/
private void downloadAPK(String apkUrl) {
Call call = OkHttpUtil.get(apkUrl);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
InputStream is;
FileOutputStream fos;
// 获取输入流
is = response.body().byteStream();
// 获取文件大小
long l = response.body().contentLength();
File file = new File(Environment.getExternalStorageDirectory(), "edj.apk");// 设置路径
fos = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch = -1;
int process = 0;
while ((ch = is.read(buf)) != -1) {
fos.write(buf, 0, ch);
process += ch;
}
fos.flush();
if (fos != null) {
fos.close();
is.close();
}
apkPath = file.getAbsolutePath();
LogUtil.LogI(l + "文件路径:" + apkPath);
// 进行安装
mHandler.sendEmptyMessage(0);
}
}
});
好了,到现在为止,我们已经将apk下载到本地,路径也已经拿到了,然后我们借助handler发送了一个message,进行apk的安装。
需要注意的是:service需要再AndroidMainfest.xml中进行注册。否则,你会发现service是没有任何响应的。
<service android:name=".service.DownLoadService" />
第四 步: 我们接到了handler发来的apk已经下载完成的message,便执行了安装APK的方法:
/**
* apk安装
*/
private void installAPK(){
//安装
Intent i = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file://"+apkPath);
i.setDataAndType(uri,"application/vnd.android.package-archive");
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
// 结束当前service
stopSelf();
}
这是固定写法,此时会进入到APK 安装页面,用户点击确定,就可以完成APK的安装。
当然,我们的apk安装还是过于简答,很多apk的更新都会再通知栏有个下载进度的提示,那是采用DownloadManager的方式来下载的。后续有空,我会再来更新一篇Down'loadManager的使用。
附Service的全部代码:
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import com.esptv.shanxi_yidong.http.OkHttpUtil;
import com.esptv.shanxi_yidong.utils.LogUtil;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
* Created by ZhangYanPeng on 2018/9/27.
*/
public class DownLoadService extends Service {
private String apkPath;
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
if (!TextUtils.isEmpty(apkPath)){
installAPK();
}
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
LogUtil.LogI("onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle bundle = intent.getExtras();
String apkUrl = bundle.getString("apkUrl");
downloadAPK(apkUrl);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
/**
* apk下载
* @param apkUrl
*/
private void downloadAPK(String apkUrl) {
Call call = OkHttpUtil.get(apkUrl);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
InputStream is;
FileOutputStream fos;
// 获取输入流
is = response.body().byteStream();
// 获取文件大小
long l = response.body().contentLength();
File file = new File(Environment.getExternalStorageDirectory(), "edj.apk");// 设置路径
fos = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch = -1;
int process = 0;
while ((ch = is.read(buf)) != -1) {
fos.write(buf, 0, ch);
process += ch;
}
fos.flush();
if (fos != null) {
fos.close();
is.close();
}
apkPath = file.getAbsolutePath();
LogUtil.LogI(l + "文件路径:" + apkPath);
// 进行安装
mHandler.sendEmptyMessage(0);
}
}
});
}
/**
* apk安装
*/
private void installAPK(){
//安装
Intent i = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file://"+apkPath);
i.setDataAndType(uri,"application/vnd.android.package-archive");
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
// 结束当前service
stopSelf();
}
}