在Webview中下载文件时,在通知中缺少下载进度栏

时间:2017-08-15 11:54:11

标签: java android eclipse android-studio webview

我能够从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>

任何帮助都会受到很大的赞赏。对于记录,下载链接是从Google驱动器生成的

我的主要活动文件以:

开头
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);

1 个答案:

答案 0 :(得分:1)

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

  1. Read file from SDCard
  2. 然后在应用程序的默认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);