我创建了一个可以下载文件的Android应用。
我使用Groundy library来支持该服务。我从this创建了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?
答案 0 :(得分:0)
您可以在返回活动时检查服务是否仍在运行。我是怎么做的是我使用单例网络类。在这里,我定义了Groundy创建函数中使用的回调对象。此单例还跟踪任务的状态(在取消,完成或失败时运行或未运行)。再次创建活动时,此单例类仍然存在,因此仍会触发进度回调。您唯一需要确保的是将新活动的监听器重新注册到现有的单例。这是一个简单的例子..
Singleton类:
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);
}
}
...