1

我創建了一個可以下載文件的Android應用程序。
我用Groundy library來支持這項服務。我從this創建NotiThread(但我編輯它支持我的要求)..
這裏是我的活動代碼...
從當前正在運行的服務中恢復Activity中的ProgressBar

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; 
    } 
} 
} 

我想問,h當進入活動和通知時,恢復活動以更新ProgressBar從當前正在運行的服務?

+0

這正是我的問題太多。 – NullPointer 2013-03-05 23:34:44

回答

0

您可以在返回活動時檢查服務是否仍在運行。我如何做是我使用單身網絡類。在這裏我定義了Groundy create函數中使用的回調對象。這個單例還會跟蹤任務的狀態(運行,或者在取消,完成或失敗時不運行)。當活動再次創建時,這個單例類仍然存在,所以進度回調仍然被觸發。你唯一需要確定的是重新註冊你的聽衆從新的活動到現有的單身人士。這裏有一個簡單的例子..

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); 
     } 
     } 
     ... 
相關問題