1

我创建了一个可以下载文件的 Android 应用程序。
我使用Groundy 库来支持该服务。我由此创建了 NotiThread 但我对其进行了编辑以支持我的要求)。
这是我的活动代码...

public class PacksActivity extends Activity {

private Context context;
private RelativeLayout download;
private ProgressBar progressBar;
private NotiThread notification;
private String url;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.packs);

    context = this;
    url = getIntent().getStringExtra("URL");

    progressBar = (ProgressBar)findViewById(R.id.progress);
    progressBar.setIndeterminate(false);
    progressBar.setMax(100);
    download = (RelativeLayout) findViewById(R.id.label_circle);
    download.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            File dest = new File(context.getFilesDir(), new File(url).getName());
            if(!dest.exists()){
                progressBar.setVisibility(View.VISIBLE);
                notification = new NotiThread(context);
                notification.run();
                Bundle extras = new Bundler().add(DownloadTemplate.PARAM_URL, url).build();
                Groundy.create(context, DownloadTemplate.class)
                        .receiver(mReceiver)
                        .params(extras)
                        .queue();
            }
        }
    });
}

private ResultReceiver mReceiver = new ResultReceiver(new Handler()) {
    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
        super.onReceiveResult(resultCode, resultData);
        switch (resultCode) {
            case Groundy.STATUS_PROGRESS:
                int progress = resultData.getInt(Groundy.KEY_PROGRESS);
                progressBar.setProgress(progress);
                if((progress % 10) == 0)notification.progressUpdate(progress);
                break;
            case Groundy.STATUS_FINISHED:
                Toast.makeText(context, "Success Download file", Toast.LENGTH_LONG);
                progressBar.setVisibility(View.GONE);
                notification.completed();
                break;
            case Groundy.STATUS_ERROR:
                Toast.makeText(context, resultData.getString(Groundy.KEY_ERROR), Toast.LENGTH_LONG).show();
                progressBar.setVisibility(View.GONE);
                notification.completed();
                break;
        }
    }
};
}

这是我的 NotiThread 课程

public class NotiThread extends Thread {

private NotificationManager mNoti = null;

private final Context mContext;
private final int mUnique;
private Notification noti;

public NotiThread(Context context) {
    super("NotiThread");
    mContext = context;
    mNoti = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mUnique = 1;
}

@Override
public void run() {
    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

    noti = new Notification(R.drawable.icon1024, "title", System.currentTimeMillis());
    noti.flags |= Notification.FLAG_ONGOING_EVENT;

    RemoteViews contentView = new RemoteViews(mContext.getPackageName(), R.layout.custom_notification);
    contentView.setTextViewText(R.id.noti_title, "title");
    contentView.setProgressBar(R.id.noti_progress, 100, 0, false);
    contentView.setTextViewText(R.id.noti_text, "0%");
    noti.contentView = contentView;

    noti.contentIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, mContext.getClass()), PendingIntent.FLAG_ONE_SHOT);

    mNoti.notify(mUnique, noti);
}

public void progressUpdate(int percentageComplete) {
    noti.contentView.setProgressBar(R.id.noti_progress, 100, percentageComplete, false);
    noti.contentView.setTextViewText(R.id.noti_text, percentageComplete + "%");
    mNoti.notify(mUnique, noti);
}

public void completed()    {
    mNoti.cancel(mUnique);
}
}

这是 GroundyTask 类

public class DownloadTemplate extends GroundyTask {

public static final String PARAM_URL = "com.app.service.PARAM_URL";

@Override
protected boolean doInBackground() {
    try {
        String url = getParameters().getString(PARAM_URL);
        File dest = new File(getContext().getFilesDir(), new File(url).getName());
        DownloadUtils.downloadFile(getContext(), url, dest, DownloadUtils.getDownloadListenerForTask(this));
        return true;
    } catch (Exception e) {
        return false;
    }
}
}

我想问,当进入活动和通知时,如何恢复活动以从当前正在运行的服务更新 ProgressBar?

4

1 回答 1

0

您可以在返回活动时检查服务是否仍在运行。我如何做到这一点是我使用单例网络类。在这里,我定义了 Groundy 创建函数中使用的回调对象。这个单例还跟踪任务的状态(正在运行,或者在取消、完成或失败时未运行)。再次创建活动时,此单例类仍然存在,因此进度回调仍在被触发。您唯一需要确保的是将您的侦听器从新活动重新注册到现有单例。这是一个简单的例子..

单例类:

public static SingletonClass getInstance(Object o) {
    if (instance == null) {
        instance = new SingletonClass();
    }
    if (o instanceof OnProgressListener) {
        progressListener = (OnProgressListener) o;
     }
    ...
    return instance;
}

回调对象:

    public Object callbackObject = new Object() {
       @OnProgress(YourGroundyTask.class)
       public void onProgress(@Param(Groundy.PROGRESS) int progress) {
        if (progressListener != null){
            progressListener.onDownloadProgress(progress);
        }
       }
       ...
于 2014-03-18T17:46:17.430 回答