0

My app uses DownloadManager to download a file , it works perfectly in my real phone.

But downloading is always pending in AVD, it never starts to download.

I'm using Android Studio 4.0.1, and I have created several AVDs, none of them works.

In the AVDs, web browsing works, so the networks should be no problem.

Besides my own app, I have tried several DownloadManager demos from github, none of them works in AVDs.

Help, please.

 class MainActivity : AppCompatActivity() {

    private val TAG = "MY"
    private var enqueue: Long = 0
    private lateinit var dm: DownloadManager

    inner class MyReceiver: BroadcastReceiver() {

        override fun onReceive(context: Context, intent:Intent) {

            Log.d(TAG, "Received.")
            val action = intent.getAction()
            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                val downloadId = intent.getLongExtra(
                        DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                val query = DownloadManager.Query();
                query.setFilterById(enqueue);
                val c = dm.query(query);
                if (c.moveToFirst()) {
                    val columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                    if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {

                        val view =  findViewById<ImageView>(R.id.imageView1)
                        val uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                        view.setImageURI(Uri.parse(uriString));
                    }
                }
            }
            if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(action)) {
                Log.d(TAG,"clicked")
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val receiver = MyReceiver()

        val f = IntentFilter()
        f.addAction( DownloadManager.ACTION_DOWNLOAD_COMPLETE)
        f.addAction( ACTION_NOTIFICATION_CLICKED)
        
        registerReceiver(receiver, f)

        btDownload.setOnClickListener {
            onClick(it)
        }
    }

     fun onClick(view: View) {
        dm =  getSystemService(DOWNLOAD_SERVICE) as DownloadManager ;

      
        val request = DownloadManager.Request(
                Uri.parse("http://www.example.com/abd3234dfdfwefwef.pdf"));
        enqueue = dm.enqueue(request);
      

    }

 
}
dzk
  • 1
  • 2

1 Answers1

0

I have solved this problem by myself.

In Android Studio (4.0.1 currently), if the AVD images is api v26+, I must set allowed network type to DownloadManager.Request.NETWORK_WIFI.

But these two don't work:

DownloadManager.Request.NETWORK_WIFI

and

DownloadManager.Request.NETWORK_WIFI + DownloadManager.Request.NETWORK_MOBILE 

dzk
  • 1
  • 2