package com.android.settings;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import java.io.File;
import android.app.PendingIntent;
import android.content.IntentSender;
import android.content.pm.PackageInstaller;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import com.android.setupwizardlib.util.WizardManagerHelper;
public class DownloadAndInstallActivity extends Activity {
private static final String TAG = "DownloadAndInstallActivity";
private static final String APKPATH = "/storage/emulated/0/Download/*******.apk";
private DownloadManager downloader;
private Uri uri;
private long reference;
private BroadcastReceiver myReceiver;
private int mSessionId =-1;
private PackageInstaller.SessionCallback mSessionCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.download_and_install_page);
mSessionCallback = new InstallSessionCallback();
getPackageManager().getPackageInstaller().registerSessionCallback(mSessionCallback);
downLoadManager();
}
@Override
protected void onDestroy() {
super.onDestroy();
getPackageManager().getPackageInstaller().unregisterSessionCallback(mSessionCallback);
unregisterReceiver(myReceiver);
}
private void downLoadManager() {
downloader = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
uri = Uri.parse("https://****/*******..apk");
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setAllowedNetworkTypes(request.NETWORK_WIFI);
request.setNotificationVisibility(request.VISIBILITY_VISIBLE);
request.setTitle("Download App");
request.setDescription("App is downloading");
request.setAllowedOverRoaming(false);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "*******.apk");
reference = downloader.enqueue(request);
IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
long referenceTo = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (referenceTo == reference) {
install();
}
}
};
registerReceiver(myReceiver, intentFilter);
}
private class InstallSessionCallback extends PackageInstaller.SessionCallback {
@Override
public void onCreated(int sessionId) {
// empty
Log.d(TAG, "onCreated()" + sessionId);
}
@Override
public void onBadgingChanged(int sessionId) {
// empty
Log.d(TAG, "onBadgingChanged()" + sessionId + "active");
}
@Override
public void onActiveChanged(int sessionId, boolean active) {
Log.d(TAG, "onActiveChanged()" + sessionId + "active" + active);
}
@Override
public void onProgressChanged(int sessionId, float progress) {
Log.d(TAG, "onProgressChanged()" + sessionId);
if (sessionId == mSessionId) {
int progres = (int) (Integer.MAX_VALUE * progress);
Log.d(TAG, "onProgressChanged" + progres);
}
}
@Override
public void onFinished(int sessionId, boolean success) {
// empty, finish is handled by InstallResultReceiver
Log.d(TAG, "onFinished()" + sessionId + "success" + success);
if (mSessionId == sessionId) {
if (success) {
Log.d(TAG, "onFinished() install success");
onNext(Activity.RESULT_OK);
} else {
Log.d(TAG, "onFinished() install failed");
}
}
}
}
public void onNext(int requestCode) {
int resultCode = Activity.RESULT_OK;
Intent intent = WizardManagerHelper.getNextIntent(getIntent(), resultCode);
try {
startActivityForResult(intent, requestCode);
} catch (ActivityNotFoundException e) {
Log.e(TAG, e.getMessage());
}
}
private IntentSender createIntentSender(Context context, int sessionId) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, sessionId,
new Intent("android.intent.action.INSTALL_COMPLETE"), PendingIntent.FLAG_IMMUTABLE/*0*/);
return pendingIntent.getIntentSender();
}
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.
OutputStream packageInSession = session.openWrite("package", 0, -1);
File file = new File(assetName);
InputStream is = new FileInputStream(file);//getAssets().open(assetName)) {
try {
byte[] buffer = new byte[16384];
int n =0;
while ((n = is.read(buffer)) >= 0) {
packageInSession.write(buffer, 0, n);
}
} catch (IOException e) {
throw new RuntimeException("Couldn't ssssssss", e);
} finally{
packageInSession.close();
is.close();
}
}
public void install (){
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
PackageInstaller.Session session = null;
try {
PackageInstaller packageInstaller = getPackageManager().getPackageInstaller();
PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
PackageInstaller.SessionParams.MODE_FULL_INSTALL);
int sessionId = packageInstaller.createSession(params);
mSessionId =sessionId;
Log.d(TAG, "sessionId = " + sessionId);
session = packageInstaller.openSession(sessionId);
addApkToInstallSession(APKPATH, session);
Log.d(TAG, "session.commit ");
session.commit(createIntentSender(getApplicationContext(), sessionId));
} catch (IOException e) {
throw new RuntimeException("Couldn't install package", e);
} catch (RuntimeException e) {
if (session != null) {
session.abandon();
}
throw e;
}
}
});
thread.start();
}
}
下载apk并自动安装
于 2024-08-22 16:00:14 首次发布