2012-06-28 46 views
0

我使用上下文變量從onReceive()方法開始了一個帶有對話框主題的活動。已啓動的活動具有帶有確定和取消按鈕的警報對話框佈局。當用戶點擊確定/取消按鈕時,我打電話給finish()方法,以使活動被破壞。但是當我啓動應用程序時,這個活動又來了。這是因爲使用上下文變量開始活動。即使我設置了FLAG_ACTIVITY_NEW_TASK,當我啓動應用程序時,它也會一次又一次地出現。有人可以幫助我,我該如何避免這種活動。使用上下文從廣播接收器開始的完成活動

編輯

public class C2DMMessageReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
      if ("com.google.android.c2dm.intent.RECEIVE".equals(intent.getAction())) { 
        Intent customDialogIntent = new Intent(context,DialogActivity.class); 
        customDialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        context.startActivity(customDialogIntent); 
      } 
    } 

} 


public class DialogActivity extends Activity{ 

     @Override 
    protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.alert_dialog); 
      TextView textAlertTitle = (TextView)findViewById(R.id.textAlertTitle); 
      TextView textAlertMessage = (TextView)findViewById(R.id.textAlertMessage); 
      Button button1Alert = (Button)findViewById(R.id.button1Alert); 
      Button button2Alert = (Button)findViewById(R.id.button2Alert); 

      textAlertTitle.setText("Notification"); 
      textAlertMessage.setText("One notification is waiting for you. Do you want to see the notification?"); 

       button1AlertOk.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         Intent intent = new Intent(DialogActivity.this,HomeActivity.class) 
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
         startActivity(); 
        } 
       }); 

    button2AlertCancel.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         finish(); 
        } 
      }); 
     } 
} 

清單

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.sample.ww.activity" android:versionCode="1" android:versionName="1.5"> 
<uses-sdk android:minSdkVersion="8" /> 
<permission android:name="com.sample.ww.activity.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" /> 
<uses-permission android:name="com.sample.ww.activity.permission.C2D_MESSAGE" /> 
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> 

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name="SplashActivity" android:theme="@android:style/Theme.NoTitleBar" 
     android:screenOrientation="portrait"> 
     <intent-filter> 
      <category android:name="android.intent.category.LAUNCHER"></category> 
      <action android:name="android.intent.action.MAIN"></action> 
     </intent-filter> 
    </activity> 
    <activity android:theme="@android:style/Theme.NoTitleBar" 
android:screenOrientation="portrait" android:name="HomeActivity"></activity> 
    <activity android:theme="@style/AlertDialog.NoTitleBar" 
      android:screenOrientation="portrait" 
      android:name="DialogActivity" 
      android:launchMode="singleInstance" 
      android:taskAffinity="com.sample.ww.activity"> 
    </activity> 
    <receiver android:name="com.nielsen.ww.receiver.C2DMMessageReceiver" 
       android:permission="com.google.android.c2dm.permission.SEND"> 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE"/> 
       <category android:name="com.sample.ww.activity" /> 
       </intent-filter> 
       <intent-filter> 
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 
        <category android:name="com.sample.ww.activity" /> 
       </intent-filter> 
    </receiver> 
+0

您是如何註冊您的BroadcastReceiver的? – nullpotent

+0

發佈您的清單並解釋哪些活動是哪個。 –

+0

BroadcastReceiver正在偵聽發送爲'sticky'的'Intent'? – Squonk

回答

1
  1. 不要使用launchMode="singleInstance"。你不需要那個。

  2. 當用戶點擊對話框中的「確定」時,您啓動HomeActivity,但您的未完成DialogActivity。這就是爲什麼當你回到應用程序時,你仍然可以看到DialogActivity。在onClick()方法中調用startActivity()之後,您需要致電finish()

  3. 對於您的所有活動,您已將taskAffinity設置爲「com.sample.ww.activity」(即使未明確指定它,它默認爲您的包的名稱爲「com」。 sample.ww.activity「)。如果你想讓這些活動在不同的任務中運行,那麼他們需要有不同的任務容量。無論如何,如果您在#2中修復了finish()問題,則可能不需要讓活動以不同的任務運行。
+0

謝謝@David,我忘了添加完成(),實際上Android本身存在問題。如果我從應用程序啓動器屏幕啓動我的應用程序,我得到推送通知後,我沒有得到此警報對話框活動,假設如果我從最近的應用程序屏幕啓動應用程序,然後我得到此警報對話框活動,並且不允許我進入應用程序。 – Venugopal

+0

你能解釋一下你正在努力完成什麼嗎?這種行爲應該是什麼?在我看來,你正在使它遠比它所需要的複雜。請更新您的問題並解釋您正在嘗試做什麼。 –

+0

Sure David,每當我從服務器收到推送通知時,我都要檢查用戶是否正在使用該應用程序。如果用戶沒有使用應用程序(意味着應用程序已關閉或在後臺),那麼我必須顯示一個警告對話框,其中包含「確定」和「查看」按鈕,我們將此稱爲應用程序外應用程序通知,如果用戶正在使用應用程序(用戶在應用程序內),那麼我只需要顯示「確定」按鈕,我們稱之爲應用程序內通知。當用戶點擊「確定」按鈕時,我只需關閉對話框(調用finish()方法)並點擊'View'啓動我的應用程序的啓動活動。 – Venugopal