Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:如何让后台和UI线程同时运行?_Android_Multithreading - Fatal编程技术网

Android:如何让后台和UI线程同时运行?

Android:如何让后台和UI线程同时运行?,android,multithreading,Android,Multithreading,我需要在非UI线程上运行一个网络进程,同时运行一个UI线程,以通知用户进程正在运行,并且应用程序没有冻结,并且在网络连接给出响应之前不允许执行下一个代码块 做这件事最好的方法是什么?是方法 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { protected Long doInBackground(URL... urls) { int count = urls.l

我需要在非UI线程上运行一个网络进程,同时运行一个UI线程,以通知用户进程正在运行,并且应用程序没有冻结,并且在网络连接给出响应之前不允许执行下一个代码块

做这件事最好的方法是什么?

是方法

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called 
             if (isCancelled()) break;
         } 
         return totalSize;
     } 

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     } 

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     } 
 } 
私有类下载文件任务扩展异步任务{
受保护的长doInBackground(URL…URL){
int count=url.length;
长totalSize=0;
for(int i=0;i
是一条路要走

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called 
             if (isCancelled()) break;
         } 
         return totalSize;
     } 

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     } 

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     } 
 } 
私有类下载文件任务扩展异步任务{
受保护的长doInBackground(URL…URL){
int count=url.length;
长totalSize=0;
for(int i=0;i
是一条路要走

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called 
             if (isCancelled()) break;
         } 
         return totalSize;
     } 

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     } 

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     } 
 } 
私有类下载文件任务扩展异步任务{
受保护的长doInBackground(URL…URL){
int count=url.length;
长totalSize=0;
for(int i=0;i
是一条路要走

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called 
             if (isCancelled()) break;
         } 
         return totalSize;
     } 

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     } 

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     } 
 } 
私有类下载文件任务扩展异步任务{
受保护的长doInBackground(URL…URL){
int count=url.length;
长totalSize=0;
for(int i=0;i
如果我想在对话框中显示加载图标,而不是publishProgress的递增函数,该怎么办?我曾尝试使用AsyncTask来完成此任务,但只获得了一个处理程序来完成此任务work@Sorrow123444在onPreExecute中显示ProgressDialog,然后在onPostExecute中关闭。如果我想在对话框中显示加载图标,而不是使用publishProgress的递增函数,该怎么办?我曾尝试使用AsyncTask来完成此任务,但只获得了一个处理程序来完成此任务work@Sorrow123444在onPreExecute中显示ProgressDialog,然后在onPostExecute中关闭。如果我想在对话框中显示加载图标,而不是使用publishProgress的递增函数,该怎么办?我曾尝试使用AsyncTask来完成此任务,但只获得了一个处理程序来完成此任务work@Sorrow123444在onPreExecute中显示ProgressDialog,然后在onPostExecute中关闭。如果我想在对话框中显示加载图标,而不是使用publishProgress的递增函数,该怎么办?我曾尝试使用AsyncTask来完成此任务,但只获得了一个处理程序来完成此任务work@Sorrow123444在onPreExecute中显示ProgressDialog,然后在onPostExecute中关闭。