【问题标题】:Download progress Bar absent in Notification when Downloading file inside Webview在 Webview 中下载文件时,通知中没有下载进度条
【发布时间】:2018-01-23 08:13:04
【问题描述】:

我可以从 WebView 下载文件,但通知面板中没有显示进度条。但是如果我转到 Ext 存储的下载目录,我可以看到该文件。

如何使下载进度条可见。任何帮助将不胜感激。

这是库中启动下载的代码

webView.setDownloadListener(new DownloadListener() {
                    @Override
                    public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {
                        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

                        request.setMimeType(mimeType);
                        //------------------------COOKIE!!------------------------
                        String cookies = CookieManager.getInstance().getCookie(url);
                        request.addRequestHeader("cookie", cookies);
                        //------------------------COOKIE!!------------------------
                        request.addRequestHeader("User-Agent", userAgent);
                        request.setDescription("Downloading file...");
                        request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType));
                        request.allowScanningByMediaScanner();
                        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimeType));
                        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                        dm.enqueue(request);
                        Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show();
                    }
                });

我的清单文件是

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/icon"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/icon"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.NoActionBar">
    <activity
        android:name=".MainActivity"
        android:configChanges="orientation"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.TransNav">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />


            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".UpdateDialog"
        android:theme="@style/Theme.AppCompat.Dialog" />

    <service android:name=".MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
    <service android:name=".MyFirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>
    <service
        android:name=".UpdateService"
        android:enabled="true"
        android:exported="true" />

</application>

任何帮助将不胜感激。为了记录,下载链接是从谷歌驱动器生成的

我的主要活动文件以:

开头
public class MainActivity extends AppCompatActivity {

boolean doubleBackToExitPressed = false;
private TextView textView;
private BottomBar bottomBar;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    View decorView = getWindow().getDecorView();
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);

【问题讨论】:

  • 您正在检查哪个操作系统?在棉花糖下面还是在它之后?
  • 我认为您的文件已下载但您无法查看该文件是这种情况吗?
  • @FarazAhmed 是的,我注意到,文件确实在下载目录中。那我该怎么解决。因为我也有 bluestacks 模拟器是 4.4.2 版本,我的设备也是相同的版本。但是当我在bluestacks中运行时可以看到下载进度条,如果我通过我的设备运行则不存在。
  • @FarazAhmed 我已经编辑了我的问题。感谢您为我指明正确的方向。

标签: java android eclipse android-studio webview


【解决方案1】:

如果您已经下载了文件,请从 SDCard 或已下载目录中获取此文件。

  1. Read file from SDCard

然后在应用程序的默认 PDF 查看器中打开文件,如下所示

   public static void openPdfFileAsIntent(Context context, File file) {

    Intent target = new Intent(Intent.ACTION_VIEW);
    target.setDataAndType(Uri.fromFile(file), "application/pdf");
    target.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    Intent intent = Intent.createChooser(target, "Open File");
    try {
        context.startActivity(intent);
    } catch (ActivityNotFoundException e) {

        Toast.makeText(context, "Please add PDF Viewer Application", Toast.LENGTH_LONG).show();
    }
}

用法:

 openPdfFileAsIntent(MainActivity.this,file);

【讨论】:

  • 嗯,这是一个替代解决方案,但是否无法像从默认浏览器下载内容一样查看下载进度条。它真的可以帮助用户跟踪文件,下载它需要多长时间。
  • 您可以使用发布进度的自定义下载器下载文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-30
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多