- 场景
- 定制机是Android 11版本的,app使用的系统签名,在底层系统原生桌面删除掉了,将本app设置为桌面。
- 效果就是开机自动启动,不会退出。
需求
1.设备无root权限。
2.app使用系统签名。
3.app需要静默升级,不让用户手动点击升级按钮。实现
1.先加权限
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
2.安装包存放到了 "storage/emulated/0/****.apk"
//调用
PackageManagerCompatP.install(UpgradeFirmwareActivity.this,Configs.DOWN_DIR_PATH + Configs.DOWN_APK, getPackageManager());
3.具体实现
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInstaller;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;
import androidx.annotation.RequiresApi;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class PackageManagerCompatP {
private static final String TAG = PackageManagerCompatP.class.getSimpleName();
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public static void install(Context context, String apkFilePath, PackageManager packageManager) {
File apkFile = new File(apkFilePath);
PackageInstaller packageInstaller = packageManager.getPackageInstaller();
PackageInstaller.SessionParams sessionParams = new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL);
sessionParams.setSize(apkFile.length());
int sessionId = createSession(packageInstaller, sessionParams);
if (sessionId != -1) {
boolean copySuccess = copyInstallFile(packageInstaller, sessionId, apkFilePath);
Log.d(TAG,"install: " + copySuccess);
if (copySuccess) {
execInstallCommand(context, packageInstaller, sessionId);
}
}
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private static int createSession(PackageInstaller packageInstaller,
PackageInstaller.SessionParams sessionParams) {
int sessionId = -1;
try {
sessionId = packageInstaller.createSession(sessionParams);
} catch (IOException e) {
e.printStackTrace();
}
return sessionId;
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private static boolean copyInstallFile(PackageInstaller packageInstaller,
int sessionId, String apkFilePath) {
InputStream in = null;
OutputStream out = null;
PackageInstaller.Session session = null;
boolean success = false;
try {
File apkFile = new File(apkFilePath);
session = packageInstaller.openSession(sessionId);
out = session.openWrite("base.apk", 0, apkFile.length());
in = new FileInputStream(apkFile);
int total = 0, c;
byte[] buffer = new byte[65536];
while ((c = in.read(buffer)) != -1) {
total += c;
out.write(buffer, 0, c);
}
session.fsync(out);
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
closeQuietly(out);
closeQuietly(in);
closeQuietly(session);
}
return success;
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private static void execInstallCommand(Context context, PackageInstaller packageInstaller, int sessionId) {
PackageInstaller.Session session = null;
try {
session = packageInstaller.openSession(sessionId);
Intent intent = new Intent(context, InstallReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
session.commit(pendingIntent.getIntentSender());
Log.d(TAG,"execInstallCommand: ");
} catch (IOException e) {
e.printStackTrace();
} finally {
closeQuietly(session);
}
}
private static void closeQuietly(Closeable c) {
if (c != null) {
try {
c.close();
} catch (IOException ignored) {
ignored.printStackTrace();
}
}
}
}
接收安装完成消息
如果app自己静默升级的话,接收不到消息。
因为我的项目是设置为了桌面,安装完成会自动重启,不需要在代码中操作。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInstaller;
import android.util.Log;
public class InstallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("InstallReceiver", "onReceive: " + intent.getIntExtra(PackageInstaller.EXTRA_STATUS, PackageInstaller.STATUS_FAILURE));
final int status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, PackageInstaller.STATUS_FAILURE);
if (status == PackageInstaller.STATUS_SUCCESS) {
Log.d("InstallReceiver", "onReceive: true");
} else {
Log.d("InstallReceiver", "onReceive: false");
}
}
}
清单文件注册
<receiver android:name="包名.InstallReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.content.pm.extra.STATUS"/>
</intent-filter>
</receiver>
坑:
需要将 AndroidManifest.xml 中下面的标签去除
android:persistent="true"后语:代码来源于网络