DownloadManager查询始终返回null

时间:2018-06-09 20:03:17

标签: android android-cursor android-download-manager

这是我的代码:

 dm = (DownloadManager) this.getSystemService(this.DOWNLOAD_SERVICE);

    // Display the Downloads pending status (YES/NO):
    try {
        c = dm.query(new DownloadManager.Query().setFilterByStatus(
                DownloadManager.STATUS_RUNNING | DownloadManager.STATUS_PAUSED |
                        DownloadManager.STATUS_SUCCESSFUL | DownloadManager.STATUS_PENDING
        ));
        if(c.moveToFirst()) {

            Toast.makeText(this, "Count is > 0", Toast.LENGTH_LONG).show(); //Test
            downloadStat.setText("YES");
            downloadStat.setTextColor(getResources().getColor(colorAccent, getTheme()));

            c.close();
        }
        else{
            Toast.makeText(this,"Count is 0", Toast.LENGTH_LONG).show(); //Test
            downloadStat.setText("NO");
            downloadStat.setTextColor(getResources().getColor(colorGreen, getTheme()));
        }

    }
    catch(NullPointerException e){
        Log.e("CURSOR ERROR", "ERRORS");
        e.printStackTrace();
        if (!c.moveToFirst()) { // i.e. if cursor is null
            Toast.makeText(this,"Count is 0", Toast.LENGTH_LONG).show(); //Test
            downloadStat.setText("NO");
            downloadStat.setTextColor(getResources().getColor(colorGreen, getTheme()));
        }

        // For handling Wifi connectivity:
        wm = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        wm.disconnect();

        c.close();
    }
    finally {
        c.close();
    }

downloadStat是一个TextView,如果有任何下载暂停/正在运行/暂停,则显示“是”或“否”。

问题: query()方法每次都返回null,无论是否有活动下载,这反过来使得游标在moveToFirst()上返回false(使用Log.i()检查),从而错误地显示下载状态为“NO”。

我还有一个问题, - 我确实意识到有一个WifiManager使用我们可以处理我们的设备Wifi的连接状态,但有没有办法实现相同的移动网络? 启用/禁用移动网络连接?

感谢任何帮助,谢谢。

0 个答案:

没有答案
相关问题