一、概述
本篇文章主要介绍利用Okhttp实现apk下载并且自动安装,其中用的核心内容就是AccessibilityService,据说很多微信红包也是基于AccessibilityService实现的,后面有时间的话,要好好的了解一下!
二、实现效果图
三、实现代码:
MainActivity.java
package com.czhappy.autoinstall.activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.czhappy.autoinstall.R;
import com.czhappy.autoinstall.utils.OpenOtherApp;
import com.czhappy.autoinstall.utils.SpUtils;
import com.daimajia.numberprogressbar.NumberProgressBar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import java.io.File;
import okhttp3.Call;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
private String TAG = "tag";
/**
* 下载文件的button
*/
private Button mDownLoadFileBtn;
/**
* 获取安装包信息
*/
private PackageInfo mPackageInfo;
/**
* 判断是否安装了天气宝,true表示安装
*/
private boolean isIntalled = false;
private NumberProgressBar number_progress_bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
initOnListener();
}
@Override
protected void onResume() {
super.onResume();
initBtnText();
}
/**
* 初始化下载按钮显示的字体,如果安装了天气宝则显示打开,否则显示下载文件
*/
private void initBtnText() {
try {
mPackageInfo = this.getPackageManager().getPackageInfo(
"com.cz.hello", 0);
} catch (PackageManager.NameNotFoun