2010-07-19 84 views
6

我正在寫一個程序,收到短信後提供一個快速回復對話框。安卓公開對話活動,沒有打開它後面的主要活動

但是,我收到了意想不到的結果。當我收到短信時,會顯示正確的對話框活動,顯示正確的電話號碼和消息,但後面有第二項活動,即我的程序中的「默認」活動(這是我啓動我的應用時打開的)

我不希望第二個活動出現。快速回復活動應該由用戶之前做的任何事情自行提出。

的 '浮動' 的活性:

public class quickReply extends Activity { 
String mNumber, mMessage; 
TextView mMainText; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    mMainText = (TextView)findViewById(R.id.mainText); 

    try{ 
     Intent i = getIntent(); 
     Bundle extras = i.getExtras(); 

     mNumber = extras.getString("theNumber"); 
     mMessage = extras.getString("theMessage"); 
     this.setTitle("Message From:" + mNumber); 
     mMainText.setText(mMessage); 


    } catch(Exception e) { 
     mMainText.setText(e.getMessage()); 
    }  

} 

}

一個的onReceive(內部活性的呼叫)

 Intent i = new Intent(context, quickReply.class); 
    i.putExtra("theNumber", mNumber); 
    i.putExtra("theMessage", mMessage); 
    i.setFlags(
      Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(i); 

所述清單:

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".quickReply" 
       android:label="@string/app_name" 
       android:theme="@android:style/Theme.Dialog" 
       > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
     <receiver android:name=".SmsReceiver"> 
     <intent-filter> 
      <action android:name= 
       "android.provider.Telephony.SMS_RECEIVED" /> 
     </intent-filter> 
    </receiver> 

</application> 

回答

3

我發現的唯一途徑這樣的作品,在您的活動定義清單:

android:launchMode="singleInstance" 

但你必須一次在對話框關閉,以重新啓動您的主/默認的活動。注意:您將失去上次啓動的所有狀態,因此這不是理想的解決方案。

UPDATE:

,你也可以通過這樣做:

Intent.FLAG_ACTIVITY_CLEAR_TASK 

所以這裏就是我所做的:

  1. 從服務,推出開原/主要活動
  2. 使用上面的對話風格活動(主要去拜拜)。
  3. 當用戶關閉對話框,與在的onCreate(處理),並調用一個額外的意圖(IS_BACK)再次啓動主:

    moveTaskToBack(真);

這將保持對話框頂部和您的主要在堆棧後面的任務。

0

您應該將活動的任務關聯性設置爲與主要活動不同的內容。這將從主要活動中分離出來,並將其作爲單獨的任務進行跟蹤:

<activity android:name=".quickReply" 
      android:label="@string/app_name" 
      android:theme="@android:style/Theme.Dialog" 
      android:launchMode="singleTask" 
      android:taskAffinity="quickReply" 
      >