2011-11-09 11 views
1

このトピックに関して多くの質問がありますが、私の特定の質問には答えられないかもしれません。通知バーからダウンロード活動に戻る

通知バーのアクティビティとプログレスバーにプログレスバーを含むファイルをダウンロードしているときと同じように、通知プログレスバーをクリックすると、プログレスバーがダウンロードの進行とともに更新され元のアクティビティに戻ります。

私は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); 
} 

は感謝、URLまたはこれを行う市場の方法を模倣した書類。

おかげで、あなたのAndroidManifest.xmlファイルで

答えて

0

は、あなたがラインandroid:launchMode="singleTop"に戻したいアクティビティの下に追加してみてください。たとえば:

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

これが作成されているあなたの活動の新しいインスタンスを防ぎ、意志のルートあなたの活動のいずれかの現在のインスタンスに意図されます。詳細はhereを参照してください。私は私の活動のlaunchModesingleInstanceを使用していた

+0

この既に設定されている(I疲れ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

は、アクティビティは、ダウンロードと一緒に進んでプログレスバーを前面に表示されます。

私は戻って家に戻り、通知バーをクリックするだけで混乱していることが分かりました。このクリックは私の活動を破壊してしまいます。私は履歴を失ってしまいました。アプリケーションのユーザーが自分のUIメニューとボタンをナビゲートできるようにします。

私は自分のアクティビティインスタンスを保存し、後でそれを取得する必要があることを知っています。

おかげで、

関連する問題