在我的应用程序中(在同一制造商的两种类型的电视盒上运行,并且所有设备都是根目录的),我会从自己的服务器上检查我的应用程序的最新版本,如果有新版本可用,我会下载apk文件并尝试使用 pm install
指挥部。虽然我从未启动过任何安装apk的活动,但它会自动从intent打开活动 Intent intent = new Intent(Intent.ACTION_VIEW);
. 我从来没用过 Intent.ACTION_VIEW
在我的整个项目中,我假设android DownloadManager
自动打开此活动,我想阻止它这样做。知道怎么做吗?
这是我的密码:
//this is the method where I download the apk
//I'm putting only the important parts to make the question as short as possible
private void downloadApk() {
if (status == State.DOWNLOADING)
return;
setStatus(State.DOWNLOADING);
String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
String fileName = appType + versionInfo.getVersionName() + ".apk";
destination += fileName;
Uri uri = Uri.parse("file://" + destination);
String url = versionInfo.getUrl();
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Downloading new Hub version");
request.setTitle("Downloading");
request.setDestinationUri(uri);
downloadId = manager.enqueue(request);
monitorDownloadPercentage();
mContext.registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
此方法仅计算文件下载的百分比,并使用该值调用回调。此计算值在其路径上更新一个progressbar,并设置为textview的文本。
private void monitorDownloadPercentage() {
new Thread(new Runnable() {
@Override
public void run() {
downloading = true;
while (downloading) {
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(downloadId);
Cursor cursor = manager.query(q);
cursor.moveToFirst();
int bytes_downloaded = cursor.getInt(cursor
.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
int bytes_total = cursor.getInt(cursor.
getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
if (upgradeStateListener != null) {
upgradeStateListener.onDownloading((float) bytes_downloaded / bytes_total * 100);
}
if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
downloading = false;
}
}
}
}).start();
}
这是在下载完成时触发的广播接收器。我确信这个广播不会引起我的问题,因为代码中唯一可能引起它的地方是 onDownloadCompleted()
方法(它使用root权限安装apk,我已经对它的所有主体进行了注解,它仍然会弹出活动。
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
final Cursor cursor = manager.query( new DownloadManager.Query().setFilterById(downloadId));
if (cursor.moveToFirst()) {
Uri uri ;
final String downloadedTo = cursor.getString(
cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
uri = Uri.parse(downloadedTo);
Log.d(TAG, "onReceive: it's ok " + uri.getPath());
upgradeStateListener.onDownloadCompleted(uri.getPath());
}
setStatus(State.DOWNLOADED);
mContext.unregisterReceiver(this);
}
};
暂无答案!
目前还没有任何答案,快来回答吧!