0、准备
想要安装应用就需要读取APK文件,因此就需要有文件读取的权限。
<uses-permission android:name=".READ_EXTERNAL_STORAGE" />
关于APK的位置,可以自定义路径,如”根目录/Download/”下
1、系统安装
系统升级流程也就是通过系统的应用安装工具安装下载好的新应用。
在安装前要动态申请允许安装未知来源应用的权限。
<uses-permission android:name=".REQUEST_INSTALL_PACKAGES"/>
在代码中跳转到系统授权页面,手动打开安装未知应用的开关。
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
(intent, 2021);
用户授权后,开启安装的流程, 就是将应用的包名和文件路径传递给Android的安装管理器:
Intent install = new Intent(Intent.ACTION_VIEW);
File file= new File(Environment.getExternalStorageDirectory() + File.separator + "Download" + File.separator + "");
Uri apkUri = FileProvider.getUriForFile(context, "", file);//在AndroidManifest中的android:authorities值
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.grantUriPermission(context.getPackageName(), apkUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
install.setDataAndType(apkUri, "application/-archive");
context.startActivity(install);
之后就会弹出弹窗,点击安装即可。
2、静默安装
静默安装不需要用户授权安装未知应用,也不需要用户手动点击同意安装,可以在后台静悄悄的安装好应用。
需要权限:
<uses-permission android:name=".INSTALL_PACKAGES"/>
此权限需要拥有系统签名或者是系统App才能使用。
public static final String PACKAGE_INSTALLED_ACTION =
".SESSION_API_PACKAGE_INSTALLED";
PackageInstaller.SessionCallback callback = new PackageInstaller.SessionCallback() {
@Override
public void onCreated(int sessionId) {
Log.e(TAG, "Install Start sessionId-> " + sessionId);
}
@Override
public void onBadgingChanged(int sessionId) {
}
@Override
public void onActiveChanged(int sessionId, boolean active) {
}
@Override
public void onProgressChanged(int sessionId, float progress) {
}
@Override
public void onFinished(int sessionId, boolean success) {
if (success) {
Log.e(TAG, "Silent Install Success");
} else {
Log.e(TAG, "Silent Install Fail");
}
}
};
private void installApk() {
PackageInstaller.Session session = null;
try {
PackageInstaller packageInstaller = getPackageManager().getPackageInstaller();
packageInstaller.registerSessionCallback(callback);
PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
PackageInstaller.SessionParams.MODE_FULL_INSTALL);
int sessionId = packageInstaller.createSession(params);
session = packageInstaller.openSession(sessionId);
addApkToInstallSession("", session);
// Create an install status receiver.
Context context = UpdateActivity.this;
Intent intent = new Intent();
intent.setAction(PACKAGE_INSTALLED_ACTION);
//此处也可以使用getService回调通知
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
IntentSender statusReceiver = pendingIntent.getIntentSender();
// Commit the session (this will start the installation workflow).
session.commit(statusReceiver);
} catch (IOException e) {
throw new RuntimeException("Couldn't install package", e);
} catch (RuntimeException e) {
if (session != null) {
session.abandon();
}
throw e;
}
}
private void addApkToInstallSession(String assetName, PackageInstaller.Session session)
throws IOException {
// It's recommended to pass the file size to openWrite(). Otherwise installation may fail
// if the disk is almost full.
try (OutputStream packageInSession = session.openWrite("package", 0, -1);
InputStream is = getAssets().open(assetName)) {
byte[] buffer = new byte[16384];
int n;
while ((n = is.read(buffer)) >= 0) {
packageInSession.write(buffer, 0, n);
}
}
}
可以注册一个监听器,接收安装的信息:
```java
mReceiver = new InstallStatusBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(PACKAGE_INSTALLED_ACTION);
registerReceiver(mReceiver,filter);
class InstallStatusBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "InstallStatusBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
Log.d(TAG,"OnReceice");
if (PACKAGE_INSTALLED_ACTION.equals(intent.getAction())) {
int status = extras.getInt(PackageInstaller.EXTRA_STATUS);
String message = extras.getString(PackageInstaller.EXTRA_STATUS_MESSAGE);
switch (status) {
case PackageInstaller.STATUS_PENDING_USER_ACTION:
break;
case PackageInstaller.STATUS_SUCCESS:
Log.d(TAG, "Install succeeded!");
break;
case PackageInstaller.STATUS_FAILURE:
case PackageInstaller.STATUS_FAILURE_ABORTED:
case PackageInstaller.STATUS_FAILURE_BLOCKED:
case PackageInstaller.STATUS_FAILURE_CONFLICT:
case PackageInstaller.STATUS_FAILURE_INCOMPATIBLE:
case PackageInstaller.STATUS_FAILURE_INVALID:
case PackageInstaller.STATUS_FAILURE_STORAGE:
Log.e(TAG, "Install failed!" + status + ", " + message);
break;
default:
Log.e(TAG, "Unrecognized status received from installer: " + status);
}
}
}
}
3、静默安装后的自启动
静默安装后,不会自动打开应用,因此需要代码打开应用。
通过注册一个广播接收器,接收系统发出的MY_PACKAGE_REPLACED广播
<receiver android:name=".UpgradeReceiver">
<intent-filter>
<action android:name=".MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>
收到广播后,启动一个前台服务:
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive: " + intent.getAction());
Log.d(TAG, "restart");
context.startForegroundService(new Intent(context, MyForegroundService.class));
}
在前台服务中,拉起应用本身的Activity。
public class MyForegroundService extends Service {
private static final String TAG = "MyForegroundService";
private static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
private BroadcastReceiver mBraviaReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "onReceive");
if(intent.getAction().equals("upgrade_success")){
Log.e(TAG, "notification_click");
Intent intent1 = new Intent();
intent1.setPackage(getPackageName());
intent1.setAction(".START_ACITVITY");
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent1);
}
}
};
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onCreate() {
super.onCreate();
setForeground();
IntentFilter filter = new IntentFilter();
filter.addAction("upgrade_success");
registerReceiver(mBraviaReceiver, filter);
Log.e(TAG, "onCreate");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.e(TAG, "onBind");
return null;
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand()");
Intent intent1 = new Intent();
intent1.setAction("upgrade_success");
sendBroadcast(intent1);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
unregisterReceiver(mBraviaReceiver);
super.onDestroy();
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void setForeground() {
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Notification channels are only supported on Android O+.
NotificationChannel notificationChannel = new NotificationChannel(
getString(R.string.app_name),
getString(R.string.app_name),
NotificationManager.IMPORTANCE_DEFAULT);
if (mNotificationManager != null) {
mNotificationManager.createNotificationChannel(notificationChannel);
Notification notification = new Notification.Builder(this.getApplicationContext(), getString(R.string.app_name))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.app_name))
.build();
startForeground(NOTIFICATION_ID, notification);
} else {
Log.d(TAG, "no NotificationManager!");
}
}
}