工具类
public class InstallUtil {
private Activity mAct;
private String mPath;//下载下来后文件的路径
public static int UNKNOWN_CODE = 2018;
public InstallUtil(Activity mAct, String mPath) {
this.mAct = mAct;
this.mPath = mPath;
}
public void install(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) startInstallO();
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) startInstallN();
else startInstall();
}
/**
* android1.x-6.x
*/
private void startInstall() {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(Uri.parse("file://" + mPath), "application/vnd.android.package-archive");
install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mAct.startActivity(install);
}
/**
* android7.x
*/
private void startInstallN() {
//参数1 上下文, 参数2 在AndroidManifest中的android:authorities值, 参数3 共享的文件
Uri apkUri = FileProvider.getUriForFile(mAct, Constants.AUTHORITY, new File(mPath));
Intent install = new Intent(Intent.ACTION_VIEW);
//由于没有在Activity环境下启动Activity,设置下面的标签
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//添加这一句表示对目标应用临时授权该Uri所代表的文件
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
install.setDataAndType(apkUri, "application/vnd.android.package-archive");
mAct.startActivity(install);
}
/**
* android8.x
*/
@RequiresApi(api = Build.VERSION_CODES.O)
private void startInstallO() {
boolean isGranted = mAct.getPackageManager().canRequestPackageInstalls();
if (isGranted) startInstallN();//安装应用的逻辑(写自己的就可以)
else new AlertDialog.Builder(mAct)
.setCancelable(false)
.setTitle("安装应用需要打开未知来源权限,请去设置中开启权限")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface d, int w) {
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
mAct.startActivityForResult(intent, UNKNOWN_CODE);
}
})
.show();
}
/**
* 检查APP是否安装
*
* @param context
* @param pkgName
* @return
*/
public static boolean checkAppInstalled(Context context, String pkgName) {
if (pkgName == null || pkgName.isEmpty()) {
return false;
}
final PackageManager packageManager = context.getPackageManager();
List<PackageInfo> info = packageManager.getInstalledPackages(0);
if (info.isEmpty())
return false;
for (int i = 0; i < info.size(); i++) {
if (pkgName.equals(info.get(i).packageName)) {
return true;
}
}
return false;
}
}
AndroidManifest.xml配置文件,添加广播介绍,添加监听的权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- 这个权限很重要 没这个权限 接收不到应用广播 -->
<uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
注册广播
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" /> <!-- add -->
<action android:name="android.intent.action.PACKAGE_REPLACED" /> <!-- update -->
<action android:name="android.intent.action.PACKAGE_REMOVED" /> <!-- delete -->
<action android:name="android.intent.action.PACKAGE_RESTARTED" /> <!-- restart -->
<action android:name="android.intent.action.BOOT_COMPLETED" /> <!-- completed -->
<data android:scheme="package" />
</intent-filter>
</receiver>
/**
* 程序卸载,安装,更新的广播监听器
*/
public class BootReceiver extends BroadcastReceiver {
private final String tag = "BootReceiver";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String packageName = intent.getDataString().substring(8);
Log.d(tag, "com.example.myapplication.BootReceiver onReceive():接收到Intent.getAction() = " + intent.getAction() + " , 包名 = " + intent.getDataString());
/**
* 接收安装广播
* android.intent.action.PACKAGE_ADDED
* */
if (action.equals(Intent.ACTION_PACKAGE_ADDED)) {
Log.d(tag, "com.example.myapplication.BootReceiver onReceive():安装了:" + packageName + "包名的程序");
}
/**
* 接收卸载广播
* android.intent.action.PACKAGE_REMOVED
* */
if (action.equals(Intent.ACTION_PACKAGE_REMOVED)) {
Log.d(tag, "com.example.myapplication.BootReceiver onReceive():卸载了:" + packageName + "包名的程序");
}
/**
* 接收更新广播
* android.intent.action.PACKAGE_REPLACED
* */
if (action.equals(Intent.ACTION_PACKAGE_REPLACED)) {
Log.d(tag, "com.example.myapplication.BootReceiver onReceive():更新了:" + packageName + "包名的程序,context.getPackageName()=" + context.getPackageName());
}
/**
* 接收重启广播
* android.intent.action.PACKAGE_REPLACED
* */
if (action.equals(Intent.ACTION_PACKAGE_RESTARTED)) {
Log.d(tag, "com.example.myapplication.BootReceiver onReceive():重启了:" + packageName + "包名的程序");
}
/**
* 接收开机广播
* android.intent.action.BOOT_COMPLETED
* */
if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.d(tag, "com.example.myapplication.BootReceiver onReceive():仪器开机,开启了:" + packageName + "包名的程序");
}
}
}
在使用的时候注册和销毁广播:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
registerReceiverBoot()
}
override fun onDestroy() {
super.onDestroy()
if (bootReceiver != null) {
unregisterReceiver(bootReceiver)
}
}
private fun registerReceiverBoot() {
if (bootReceiver == null) {
bootReceiver = BootReceiver()
} else {
unregisterReceiver(bootReceiver)
}
val intentFilter = IntentFilter()
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED)
intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED)
intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED)
intentFilter.addDataScheme("package")
registerReceiver(bootReceiver, intentFilter)
}