2011-11-09 40 views
1

我知道很多問題都被問到這個問題,但沒有人可以回答我的具體問題,或者我沒有認出正確的答覆!返回到下載活動通知欄

就像在Android Market下載通知欄中的活動和進度欄中的進度欄​​文件期間一樣,單擊通知進度欄返回到原始活動,並且進度條更新爲下載進度。

我正在使用AsyncTask進行下載並創建&更新通知欄一切正常,但是當我點擊通知時,它將我返回到同一活動,但沒有任何數據或進度條(空白活動)換句話說,它開始新的活動並放棄額外活動和所有字段數據,包括活動進度條狀態。

我試着玩旗幟和意圖,但沒有得到我原來的活動加載時單擊通知欄特別是當回到其他活動,然後去主屏幕,然後按通知欄。

我運行下載方式:

df = new AsyncDownloadTask(); 
df.execute(m_item.getiid(),m_item.getipackage()); 

和片段(我改變了通知欄只是爲了顯示進度文本):

protected void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState); 
setContentView (R.layout.activity_app_display); 
//get extras 
Bundle extras = getIntent().getExtras(); 

    if (extras == null) { 
     return; 
    } 
    itemId = extras.getString("id"); 
    itemStatus = extras.getString("IorU"); 
    ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressbar); 
    progressBar.setVisibility(0); 

    showUI(); 

} 
private void showUI() { 

if(m_item != null) { 

    TextView actvTitle = (TextView) findViewById(R.id.title_text); 
    actvTitle.setText(m_item.geticategory()); 

    appinstall = (Button) findViewById(R.id.app_install_btn); 
    appudinstall = (Button) findViewById(R.id.app_udinstall_btn); 

    permlist = (LinearLayout) findViewById(R.id.app_perm); // 
    info = (LinearLayout) findViewById(R.id.info); 
    mList = (HeightListView) findViewById(R.id.app_perm_list); 
    mList.setTextFilterEnabled(true); 


    instTxt = (TextView) findViewById(R.id.app_install); 

    TextView titleTxt = (TextView) findViewById(R.id.app_titlebig); 
    TextView companyTxt = (TextView) findViewById(R.id.app_descbig); 

    TextView descTxt = (TextView) findViewById(R.id.long_desc); 
    TextView verTxt = (TextView) findViewById(R.id.version); 

    //fill info 
    titleTxt.setText(m_item.getititle()); 
    companyTxt.setText(m_item.geticompany()); 
    descTxt.setText(m_item.getidescription()); 
    verTxt.setText(" Version:" + m_item.getiversion()); 


} 
    } 

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

@Override 
public Object onRetainNonConfigurationInstance() { 
Toast.makeText (getApplicationContext(), "onRetainNonConfigurationInstance", Toast.LENGTH_SHORT).show(); 
return null; 
} 
@Override 
protected void onDestroy() { 
super.onDestroy(); 
/* 
if(df != null) 
{ 
    if(!df.isCancelled()) 
    df.cancel(true); 
} 
CN=true; 
*/ 

} 
@Override 
protected void onPause() { 
super.onPause(); 
} 
@Override 
protected void onStart() { 
super.onStart(); 
} 



private class AsyncDownloadTask extends AsyncTask<String, Integer, Void> 
    { 
    private int successCount; 
    private int numTotalFiles; 
    private File outputFile; 
    private String pack; 
    private int downloadErr = 0; 


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

successCount = 0; 
notificationManager = (NotificationManager) getApplicationContext().getSystemService(getApplicationContext().NOTIFICATION_SERVICE); 
CN=false; 
    } 


    @Override 
    protected Void doInBackground(String... params) 
    { 
    String remoteFilepath; 
String id = params[0]; 
pack = params[1]; 
    remoteFilepath = "http://www.myserver/download/1.zip"; 

    String PATH = Environment.getExternalStorageDirectory()+ "/download/"; 
    File file = new File(PATH); 
    file.mkdirs(); 
    try 
    { 
    if(isCancelled()) 
     return null; 

    URL url = new URL(remoteFilepath); 
    HttpURLConnection c = (HttpURLConnection) url.openConnection(); 
    c.setConnectTimeout(getConnectTimeout()); 
    c.setReadTimeout(getReadTimeout()); 
    c.setRequestMethod("GET"); 
    c.setDoOutput(true); 
    c.connect(); 
    int filesize = c.getContentLength(); 
    if(filesize > 0) 
    { 

     outputFile = new File(file, "1.zip"); 
     FileOutputStream fos = new FileOutputStream(outputFile); 

     InputStream is = c.getInputStream(); 
     int bytesRead, totalBytesRead = 0; 
     byte[] bytes = new byte[BYTES_BUFFER_SIZE]; 
     String progress, kbytes; 
     while(!isCancelled() && (bytesRead = is.read(bytes)) != -1) 
     { 
     totalBytesRead += bytesRead; 
     fos.write(bytes, 0, bytesRead); 
     if(!isCancelled()) //&& loopCount++ % 20 == 0) 
     { 
      RemoteViews progressView = getProgressView(successCount + 1, numTotalFiles, totalBytesRead, filesize); 
      if(progressView == null) 
      { 
      progress = "Download "+pack; 
      kbytes = String.format("%s/%s", getStringByteSize(totalBytesRead), getStringByteSize(filesize)); 

      if(!isCancelled() && !CN){ 
       showNotification("Downloading File(s)", progress , kbytes); 
       publishProgress(totalBytesRead,filesize); 
      } else { return null; } 
      } 
      else 
      { 

      if(!isCancelled() && !CN){ 
       showNotification(progressView, "Downloading File(s)"); 
      } else { return null; } 
      } 

     } 
     } 
     fos.close(); 
     is.close(); 
     if(isCancelled()) 
     return null; 

     successCount ++; 
    } 
    else 
    { 
    } 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    showNotification("Download Failed", "Download Progress", "Failed: " + (new File(remoteFilepath)).getName()); 
    //updateCancelInstall(); 
    publishProgress(100,100); 
    notificationManager.cancel(42); 
    downloadErr = 1; 
    CN = true; 
    } 

return null; 
    } 

    @Override 
    protected void onCancelled() 
    { 
super.onCancelled(); 
showNotification("Download Cancelled", "Download Progress", "Cancelled"); 
CN = true; 
publishProgress(100,100); 
notificationManager.cancel(42); 
    } 

    protected void onProgressUpdate(Integer... prog) { 
    if(prog[0]<prog[1]){ 
     updateProgress(prog[0],prog[1],false); 
    } else { 
     updateProgress(100,100,true); 
     notificationManager.cancel(42); 
    } 
    } 


    @Override 
protected void onPostExecute(Void result) 
{ 
super.onPostExecute(result); 
if(!CN){ 
    if(downloadErr==0) { 
     showNotification("Installing", "Installing "+pack, "Completed"); 
     updateDLcount udl = new updateDLcount(); 
     udl.execute("hello"); 
     //updateCancelInstall(); 
     notificationManager.cancel(42); 
    } else { 

     showNotification("Download Error", "Failed to download "+pack, "Error"); 
     //updateCancelInstall(); 
     notificationManager.cancel(42); 
     } 
    } 
} 
} 

    protected RemoteViews getProgressView(int currentNumFile, int totalNumFiles, int currentReceivedBytes, int totalNumBytes) 
    { 
    return null; 
    } 



    protected Class<?> getIntentForLatestInfo() 
    { 
return DisplayApp.class; 
    } 

protected void showNotification(String ticker, String title, String content) 
{ 

    Notification notification = new Notification(R.drawable.download_icon, ticker, System.currentTimeMillis()); 
    Intent i=new Intent(this, DisplayApp.class); 
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP); 
i.putExtra("id", itemId); 
i.putExtra("IorU", "itemStatus"); 
i.putExtra("progrss", content); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, 0); 
    notification.setLatestEventInfo(getApplicationContext(), title, content, contentIntent); 
    notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT; 

    notificationManager.notify(42, notification); 
if(content.equalsIgnoreCase("Cancelled")||content.equalsIgnoreCase("Completed")||CN)notificationManager.cancel(42); 
} 
protected void updateProgress(int downsize, int totalsize, boolean cancel){ 

//Log.i("YYYY","cancel="+cancel); 

if(!cancel || (downsize<totalsize)){ 
    ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressbar); 
    TextView pb_t = (TextView) findViewById(R.id.progressbar_text); 
    progressBar.setVisibility(0); 
    pb_t.setVisibility(0); 
    progressBar.setProgress((int)downsize*100/totalsize); 
    pb_t.setText(String.format("%s/%s", getStringByteSize(downsize), getStringByteSize(totalsize))); 
} else { 
    ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressbar); 
    TextView pb_t = (TextView) findViewById(R.id.progressbar_text); 
    progressBar.setProgress(100); 
    progressBar.setVisibility(4); 
    pb_t.setVisibility(4); 
    updateCancelInstall(); 
} 
} 


protected void showNotification(RemoteViews remoteView, String ticker) 
{ 
    Notification notification = new Notification(R.drawable.download_icon, ticker, System.currentTimeMillis()); 
    notification.contentView = remoteView; 
    Intent i=new Intent(this, DisplayApp.class); 
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    notification.contentIntent = PendingIntent.getActivity(this, 0, i, 0); 
    //Log.d("YYYY","2:"+notification.contentIntent.toString()); 
    notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT; 

    notificationManager.notify(42, notification); 
} 

感激,如果你可以通過代碼幫助我,網址或者模仿市場這種做法的任何文件。

感謝

回答

0

在你AndroidManifest.xml文件試試你想返回的行android:launchMode="singleTop"活動下添加。例如:

<activity android:name=".MainLayout" android:label="@string/app_name" android:launchMode="singleTop">

這將防止創建您的活動的一個新實例,將路由的意圖你活動的任何現有實例。欲瞭解更多信息,請參閱here

+0

這已經設置(我累了singletop,singletask singleinstance),仍然不能防止新的實例創建<活動機器人:名字=機器人 「DisplayApp」:configChanges = 「方向| keyboardHidden」 機器人:theme =「@ style/Theme.D1」android:launchMode =「singleTop」android:label =「@ string/title」> – BBAND

+0

嗯,在這種情況下,看起來你已經做了所有我能想到的事情,問題,道歉我不能有更多的幫助。 – Leyths

+0

看來我需要保存活動的實例,然後按照文檔[這裏](http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle %29),如果是的話,我需要一些代碼幫助如何保存我的活動視圖並恢復它? – BBAND

1

我在活動launchMode使用singleInstance,活動涉及到前面進度條的下載一起進步。

我發現只有當我搬回家後纔會感到困惑,然後點擊通知欄,這點很明顯,因爲點擊後會破壞我的活動,所以我的工作就是在我的活動中禁用後退鍵讓應用程序用戶瀏覽我的UI菜單和按鈕。

我知道那裏應該有存儲我的活動實例,並在以後檢索它,無論歷史/堆棧發生了什麼,但到目前爲止,上述對我來說已經足夠了!

感謝,