嘿伙计们,我正在制作这个简单的应用程序来获取存储在客户端服务器上的 apk。 我下载了文件,我想打开它进行安装。
所以第一个问题是它下载了两次? 其次是我在打开应用程序时遇到错误解析应用程序?但是如果我从手机打开文件,它可以工作。
第一次从我的适配器下载:
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
.mkdirs();
DownloadManager manager = (DownloadManager) context.getSystemService (Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request (Uri.parse (appUrlString));
request.setAllowedNetworkTypes (DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
request.setDescription ("Downloading");
request.setTitle (appName);
request.allowScanningByMediaScanner ();
request.setNotificationVisibility (
DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir (Environment.DIRECTORY_DOWNLOADS, appName+".apk");
manager.enqueue (request);
mCallback.onClick (String.valueOf (manager.enqueue (request)),appName);
}
});
从我的主要活动打开文件:
private BroadcastReceiver onDownloadComplete = new BroadcastReceiver () {
@Override
public void onReceive(Context context, Intent intent) {
//Fetching the download id received with the broadcast
long id = intent.getLongExtra (DownloadManager.EXTRA_DOWNLOAD_ID, -1);
spinner.setVisibility (View.GONE);
Toast.makeText (context, "Download Completed", Toast.LENGTH_SHORT).show ();
Log.d ("INTENT ", String.valueOf (id) + "DID " + downloadID);
openDownloadedAttachment(context, id);
}
};
@Override
public void onClick(String value, String fileName) {
spinner.setVisibility (View.VISIBLE);
Log.d ("INTENT REC", value + " " + fileName);
fileNameDownloaded = fileName;
downloadID = value;
}
private void openDownloadedAttachment(final Context context, final long downloadId) {
DownloadManager downloadManager = (DownloadManager) context.getSystemService (Context.DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query ();
query.setFilterById (downloadId);
Cursor cursor = downloadManager.query (query);
if (cursor.moveToFirst ()) {
int downloadStatus = cursor.getInt (cursor.getColumnIndex (DownloadManager.COLUMN_STATUS));
String downloadLocalUri = cursor.getString (cursor.getColumnIndex (DownloadManager.COLUMN_LOCAL_URI));
String downloadMimeType = cursor.getString (cursor.getColumnIndex (DownloadManager.COLUMN_MEDIA_TYPE));
if ((downloadStatus == DownloadManager.STATUS_SUCCESSFUL) && downloadLocalUri != null) {
openDownloadedAttachment (context, Uri.parse (downloadLocalUri), downloadMimeType);
}
}
cursor.close ();
}
private void openDownloadedAttachment(final Context context, Uri attachmentUri, final String attachmentMimeType) {
if (attachmentUri != null) {
// Get Content Uri.
if (ContentResolver.SCHEME_FILE.equals (attachmentUri.getScheme ())) {
// FileUri - Convert it to contentUri.
File file = new File (attachmentUri.getPath ());
attachmentUri = FileProvider.getUriForFile (MainActivity.this, "com.handshake.downloadmanager.provider", file);
System.out.println ("Opening: " + attachmentUri);
}
Intent openAttachmentIntent = new Intent (Intent.ACTION_VIEW);
openAttachmentIntent.setDataAndType (attachmentUri, attachmentMimeType);
openAttachmentIntent.setFlags (Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
context.startActivity (openAttachmentIntent);
System.out.println ("Opening attachement: " + openAttachmentIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText (context, context.getString (R.string.unable_to_open_file), Toast.LENGTH_LONG).show ();
}
}
}
我有一个界面,告诉我的主活动下载的 id 是什么以及何时完成。
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external_files"
path="." />
</paths>
provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provide_paths" />
</provider>
那么为什么它会下载 2 次? 以及尝试打开它时的错误消息?
编辑: 我用包错误的错误消息解决了一个问题。需要打开应用设置到所有应用安装完毕
private void askSettings(){
Intent intent = new Intent ();
intent.setAction (Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
Uri uri = Uri.fromParts ("package", this.getPackageName (), null);
intent.setData (uri);
this.startActivity (intent);
}
但是下载2次是我头痛
的哈哈太愚蠢的错误了。哈哈
我将请求 ID 发送到我的接口,但发送方式
mCallback.onClick (String.valueOf (manager.enqueue (request)),appName);
我再次呼叫排队。.大声笑只需要提出请求..呃,谢谢伙计们