Android 带有ProgressBar的活动->;服务->;异步下载任务-但是如何更新进度?

Android 带有ProgressBar的活动->;服务->;异步下载任务-但是如何更新进度?,android,service,progress-bar,Android,Service,Progress Bar,这是当前的状态/情况:我有一个活动,它绑定了一个服务,该服务创建异步任务,下载各种web资源。这很有效,但ProgressBar当然没有显示任何内容 以前我有一个活动,创建了一个异步任务,下载了一些东西。AsyncTask获得了保存ProgressBar的视图。所以我可以使用onProgressUpdate和publishProgress更新进度。显然,这不再有效,因为我没有参考ProgressBar 那么,你知道如何更新进度吗 提前感谢。让服务通知正在运行的活动有关onProgressUpda

这是当前的状态/情况:我有一个活动,它绑定了一个服务,该服务创建异步任务,下载各种web资源。这很有效,但ProgressBar当然没有显示任何内容

以前我有一个活动,创建了一个异步任务,下载了一些东西。AsyncTask获得了保存ProgressBar的视图。所以我可以使用onProgressUpdate和publishProgress更新进度。显然,这不再有效,因为我没有参考ProgressBar

那么,你知道如何更新进度吗


提前感谢。

服务
通知正在运行的
活动
有关
onProgressUpdate()
的进度。这可以通过广播
意图
,或者通过
活动注册的回调对象
(当
活动
被销毁时,例如在屏幕旋转时,将其注销)。

非常好地解释了这一点,但实际上忽略了示例:) 我去扔了,就在这里

public class Detail extends Activity {

    private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(DownloadService.CUSTOM_INTENT)) {
                mProgressDialog.setProgress(intent.getFlags());
            }
        }
    };

    // Flag if receiver is registered 
    private boolean mReceiversRegistered = false;
    // Define a handler and a broadcast receiver
    private final Handler mHandler = new Handler();

    @Override
    protected void onResume() {
      super.onResume();

      // Register Sync Recievers
      IntentFilter intentToReceiveFilter = new IntentFilter();
      intentToReceiveFilter.addAction(DownloadService.CUSTOM_INTENT);
      this.registerReceiver(mIntentReceiver, intentToReceiveFilter, null, mHandler);
      mReceiversRegistered = true;
    }

    @Override
    public void onPause() {
      super.onPause();

      // Make sure you unregister your receivers when you pause your activity
      if(mReceiversRegistered) {
        unregisterReceiver(mIntentReceiver);
        mReceiversRegistered = false;
      }
    }
}








public class DownloadService extends Service {
    private static final String CLASS_NAME = DownloadService.class.getSimpleName();
    private List<Download> downloads = new ArrayList<Download>();
    private int currentPosition;
    public static final String sdcardPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
    private Context ctx;
    @Override
    public IBinder onBind(Intent arg0) {
        ctx = getApplicationContext();
        return mBinder;
    }


    private class DownloadFile extends AsyncTask<String, Integer, String> {

        @Override
        protected String doInBackground(String... _url) {
            Log.d(Constants.LOG_TAG, CLASS_NAME + " Start the background GetNewsTask \nURL :" + _url[0]);
            int count;
            File finalFile = new File(sdcardPath + Constants.APK_LOCAL_PATH + "/" + splitName(_url[0]));
            try {
                if (!finalFile.exists()) {
                    Log.i(Constants.LOG_TAG, CLASS_NAME + " Donwloading apk from the Web");
                    URL url = new URL(_url[0]);
                    URLConnection conexion = url.openConnection();
                    conexion.connect();
                    // this will be useful so that you can show a tipical 0-100%
                    // progress bar
                    int lenghtOfFile = conexion.getContentLength();
                    // downlod the file
                    InputStream input = new BufferedInputStream(url.openStream());
                    File dir = new File(sdcardPath + Constants.APK_LOCAL_PATH);
                    if (!dir.exists())
                        dir.mkdirs();
                    OutputStream output = new FileOutputStream(sdcardPath + Constants.APK_LOCAL_PATH + "/" + splitName(_url[0]));
                    byte data[] = new byte[1024];
                    long total = 0;
                    while ((count = input.read(data)) != -1) {
                        total += count;
                        // publishing the progress....
                        publishProgress((int) (total * 100 / lenghtOfFile));
                        output.write(data, 0, count);
                    }
                    output.flush();
                    output.close();
                    input.close();
                } else {
                    Log.i(Constants.LOG_TAG, CLASS_NAME + " Apk in SDcard");
                    publishProgress(100);
                }
            } catch (Exception e) {
            }

            return null;

        }

        @Override
        protected void onProgressUpdate(Integer... progress) {
            Intent i = new Intent();
            i.setAction(CUSTOM_INTENT);
            i.setFlags(progress[0]);
            ctx.sendBroadcast(i);
        }
    }

    private String splitName(String url) {
        String[] output = url.split("/");
        return output[output.length - 1];
    }

    public static final String CUSTOM_INTENT = "es.tempos21.sync.client.ProgressReceiver";

    private final IDownloadService.Stub mBinder = new IDownloadService.Stub() {

        public void downloadAsynFile(String url) throws DeadObjectException {
            try {
                DownloadFile d = new DownloadFile();
                d.execute(url);
            } catch (Exception e) {
                Log.e(Constants.LOG_TAG, CLASS_NAME + " " +e.getMessage());         }
        }


        }
};


interface IDownloadService {

    void downloadAsynFile(String url);    
}
公共类详细信息扩展活动{
private final BroadcastReceiver mIntentReceiver=新的BroadcastReceiver(){
@凌驾
公共void onReceive(上下文、意图){
if(intent.getAction().equals(DownloadService.CUSTOM_intent)){
mProgressDialog.setProgress(intent.getFlags());
}
}
};
//如果接收器已注册,则标记
私有布尔值mReceiversRegistered=false;
//定义处理程序和广播接收器
私有最终处理程序mHandler=新处理程序();
@凌驾
受保护的void onResume(){
super.onResume();
//寄存器同步接收器
IntentFilter IntentReceiveFilter=新建IntentFilter();
intentoreceivefilter.addAction(下载服务.CUSTOM\u INTENT);
this.registerReceiver(minentReceiver、intentReceiveFilter、null、mHandler);
mReceiversRegistered=真;
}
@凌驾
公共无效暂停(){
super.onPause();
//请确保在暂停活动时注销接收者
如果(mReceiversRegistered){
未注册接收人(mIntentReceiver);
mReceiversRegistered=假;
}
}
}
公共类下载服务扩展服务{
私有静态最终字符串CLASS_NAME=DownloadService.CLASS.getSimpleName();
私有列表下载=新建ArrayList();
私人职位;
公共静态最终字符串sdcardPath=Environment.getExternalStorageDirectory().getAbsolutePath()+“/”;
私有上下文ctx;
@凌驾
公共IBinder onBind(意图arg0){
ctx=getApplicationContext();
返回mBinder;
}
私有类下载文件扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…\u url){
d(Constants.Log_标记,CLASS_NAME+“启动后台GetNewsTask\nURL:”+_url[0]);
整数计数;
File finalFile=新文件(sdcardPath+Constants.APK_LOCAL_PATH+“/”+splitName(_url[0]);
试一试{
如果(!finalFile.exists()){
Log.i(Constants.Log_标记,CLASS_名称+“从Web加载apk”);
URL=新URL(_URL[0]);
URLConnection conexion=url.openConnection();
conexion.connect();
//这将非常有用,以便您可以显示一个典型的0-100%
//进度条
int lenghtOfFile=conexion.getContentLength();
//下载文件
InputStream输入=新的BufferedInputStream(url.openStream());
File dir=新文件(sdcardPath+Constants.APK\u LOCAL\u PATH);
如果(!dir.exists())
dir.mkdirs();
OutputStream output=新文件OutputStream(sdcardPath+Constants.APK_LOCAL_PATH+“/”+splitName(_url[0]);
字节数据[]=新字节[1024];
长总计=0;
而((计数=输入。读取(数据))!=-1){
总数+=计数;
//发布进度。。。。
出版进度(整数)(总计*100/长办公室);
输出.写入(数据,0,计数);
}
output.flush();
output.close();
input.close();
}否则{
Log.i(Constants.Log_标签,CLASS_NAME+“SD卡中的Apk”);
出版进度(100);
}
}捕获(例外e){
}
返回null;
}
@凌驾
受保护的void onProgressUpdate(整数…进度){
意图i=新意图();
i、 设置动作(定制意图);
i、 setFlags(进度[0]);
ctx.sendBroadcast(i);
}
}
私有字符串拆分名称(字符串url){
字符串[]输出=url.split(“/”);
返回输出[output.length-1];
}
公共静态最终字符串CUSTOM_INTENT=“es.tempos21.sync.client.ProgressReceiver”;
私有最终IDownloadService.Stub mBinder=新IDownloadService.Stub(){
public void downloadAsynFile(字符串url)引发DeadObjectException{
试一试{
DownloadFile d=新的DownloadFile();
d、 执行(url);
}捕获(例外e){
Log.e(Constants.Log_标记,CLASS_NAME+“”+e.getMessage());}
}
}
};
接口IDownloadService{
无效下载文件(字符串url);
}

您可以通过Toast轻松显示进度,从而消除所有UI问题:

public class foo extends Service {
private Toast toast;

@SuppressLint("ShowToast")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    ctxt = getApplicationContext();
    toast = Toast.makeText(ctxt, "", Toast.LENGTH_SHORT);
    ...
}

public void functionCalledByYourAsyncWithUpdates(String progress){
    toast.setText (progress);
    toast.show;
}
}

谢谢我现在有一个ProgressReceiver类,它扩展了BroadcastReceiver。来自onProg的意图