2012-09-27 30 views
1

我注意到在舊設備上,一旦您執行標準共享意圖(fb,twitter,電子郵件等),直到實際共享對話框彈出。我想知道如何在等待此共享對話框顯示時彈出進度對話框?如何在等待共享意圖彈出時顯示進度對話框

這裏是我的代碼:

private void share(String subject,String body) { 
     Intent share = new Intent(Intent.ACTION_SEND); 
     share.setType("text/plain"); 
     share.putExtra(android.content.Intent.EXTRA_SUBJECT, title); 
     share.putExtra(android.content.Intent.EXTRA_TEXT, body); 
     startActivity(Intent.createChooser(share, "Share via")); 
    } 

回答

-1

你可以使用一個的AsyncTask(),以幫助您展示一個對話框的進步,例如,

private class TestTask extends AsyncTask<Void, Void, Void> { 

    protected void onPreExecute() { 
     //show dialog 
    } 

    protected void doInBackground(Object... params) { 
     private void share(subject,body); 
    } 

    protected void onPostExecute(Long result) { 
     //cancel dialog 
    } 
} 

new TestTask().execute(); 
啓動任務

你可以從the documentation here找到更多關於AsyncTask()的信息。

+0

爲什麼此解決方案降低了投票率? – XepterX

+0

我沒有降低您的回覆,但如果您添加了更多特定的代碼,我會加倍努力。單靠這些方法無法幫助Android n00b弄清楚AsyncTask是什麼。添加一個到android文檔的鏈接,並且實際展示如何執行asynctask,以及打開和關閉對話框將是一個很大的改進。 – Phil

+0

好吧,感謝提示 – XepterX

1

這似乎不可能 - 您無法知道意圖選擇器何時可以顯示。就你的應用而言,這似乎是一個異步操作。

爲了證實這一點,你可以嘗試定時功能:

private void share(String subject,String body) { 
    //Timing code 
    long startTime = System.currentTimeMillis(); 

    Intent share = new Intent(Intent.ACTION_SEND); 
    share.setType("text/plain"); 
    share.putExtra(android.content.Intent.EXTRA_SUBJECT, title); 
    share.putExtra(android.content.Intent.EXTRA_TEXT, body); 
    startActivity(Intent.createChooser(share, "Share via")); 

    //Timing code 
    long endTime = System.currentTimeMillis(); 
    Log.d("Test", "Time for share function: " + (endTime-startTime) + "ms"); 
} 

如果你得到一個非常小的結果,這意味着你的功能是快速執行,並瓶頸是在Android系統本身(你不能影響)。

另一方面,如果你得到500+毫秒左右的數據,可以使用XepterX和display a Progress Dialog描述的方法。

3

我想出瞭如何做到這一點。當您調用startActivity(shareIntent)時,方法中的onPause被調用。所以爲了解決這個問題,我做了以下工作:

  • 創建一個類進度對話框變量。
  • 在共享功能開始時顯示進度對話框。
  • 在的onPause我做了如下:

if(dialog.isShowing()){ 
    dialog.dismiss(); 
} 
1

我建議以下方法:

  1. 首先創建一個進度對話框並顯示它
  2. 呼叫分享活動
  3. 隱藏返回主活動的進度對話框(onResume)

下面是代碼:

ProgressDialog shareWaitDialog; 

private void shareIt() { 
    String subject="Subject"; 
    String text = "text...."; 
    shareWaitDialog = new ProgressDialog(getActivity()); 
    shareWaitDialog.setTitle("Sharing..."); 
    shareWaitDialog.setMessage(subject); 
    shareWaitDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
    shareWaitDialog.show(); 

    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
    sharingIntent.setType("text/plain"); 
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, 
      subject); 
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
      text)); 
    startActivity(Intent.createChooser(sharingIntent, 
      "Share using:")); 
} 

@Override 
public void onResume() { 
    if (shareWaitDialog != null && shareWaitDialog.isShowing()) 
     shareWaitDialog.hide(); 

    super.onResume(); 
} 
0

這似乎很難做到,而不是,您可以顯示像吐司上分享按鈕點擊「等待應用程序...」。吐司可以填補時間差距。

mShare.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 


      Toast.makeText(ReferralActivity.this, "Waiting for Apps to Send", Toast.LENGTH_SHORT).show(); 

      try { 
       String sAux = "Content To Share"; 
       Intent i = new Intent(Intent.ACTION_SEND); 
       i.setType("text/plain"); 
       i.putExtra(Intent.EXTRA_SUBJECT, "Sample"); 
       i.putExtra(Intent.EXTRA_TEXT,sAux); 
       startActivity(Intent.createChooser(i, "Share using:")); 
      } catch(Exception e) { 
       //e.toString(); 
      } 
     } 
    });