2011-10-28 10 views
0

興味深い問題があります。私はサーバーと同期し、新しい注文が来たら、通知とSHO/DISMISSボタン付きのダイアログを表示する私のアンドロイドAppのサービス実装を持っています。現在実行中のアクティビティに関係なくダイアログを表示する必要があるため、新しいオーダーが表示されたときに、サービスから開始する別のアクティビティとして実装しました。より多くのstartActivityコールがあると、ダイアログのアクティビティは1回だけ開始されます

アクティビティはビューを設定せず、ただAlertDialogを作成します。 SHOWボタンはOrdersListActivityを開始し、DISMISSボタンはダイアログを閉じます。すべてが正常に動作しますが、初めての場合のみです。ダイアログを閉じて別の注文が来たら、もう一度表示されます。しかし、SHOWボタンをクリックすると、OrdersListが表示されますが、後で別の注文が来ると、ダイアログは表示されません。しかし、logcatによると、活動が開始されます。私は何時間もこれをぶち壊してきました、あなたは私を助けてくれますか?ありがとう。ここで

は、(ブロードキャストを送信し、ダイアログ活動を開始し、通知を行う)私のサービスクラスからメソッドです:

公共ボイドnotifyNewOrder(){ここで

Context context = getApplicationContext(); 

    Intent notificationIntent = new Intent(this, OrdersService.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK); 

    newOrderNotification.setLatestEventInfo(this, getString(R.string.notification_text), getString(R.string.notification_text), contentIntent); 
    notificationManager.notify(NOTIFICATION_ID, newOrderNotification); 


    Intent intent = new Intent(NEW_ORDER_RECEIVED); 
    sendBroadcast(intent); 

    Intent dialog = new Intent(context, NotificationDialog.class); 
    dialog.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(dialog); 

} 

はのコードですNotificationDialog活性クラス:

パブリッククラスNotificationDialogアクティビティ{

private static final int DIALOG_NEW_ORDER = 0; 
private static final String LOG_TAG = "NotificationDialog"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    Log.d(LOG_TAG, "onCreate - creating activity..."); 

} 

@Override 
public void onResume() { 

    super.onResume(); 
    Log.d(LOG_TAG, "onResume - starting the dialog..."); 
    showDialog(DIALOG_NEW_ORDER); 

} 



@Override 
protected Dialog onCreateDialog(int id) { 

    Dialog dialog; 

    switch(id) { 
     case DIALOG_NEW_ORDER: 

      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setMessage(R.string.notification_dialog_new_order) 
        .setCancelable(false) 
        .setPositiveButton(R.string.notification_dialog_show, new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 

          /* 
          * Cancel the notification 
          */ 
          NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
          notificationManager.cancel(OrdersService.NOTIFICATION_ID);        

          /* 
          * Go to the list 
          */ 
          Intent listIntent = new Intent(getApplicationContext(), OrdersListActivity.class); 
          startActivity(listIntent); 

          dialog.dismiss(); 
          finish(); 

         } 

        }) 
        .setNegativeButton(R.string.notification_dialog_dismiss, new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 

          /* 
          * Cancel the notification 
          */ 
          NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
          notificationManager.cancel(OrdersService.NOTIFICATION_ID); 
          dialog.dismiss(); 
          finish(); 
         } 
        }); 

      dialog = builder.create(); 

      break;   
     default: 
      dialog = null; 
    } 

    return dialog; 
} 
を拡張

}

私はNotificationDialog活動ののonCreateとonResumeメソッドにいくつかのログを追加し、次を発見しました:

  1. NotificationDialog活動が開始された最初の時間を、これらのデバッグメッセージは
  2. をプリントアウトしています
  3. logcatは10-28 10:00:38.074:INFO/ActivityManager(63):Starting:Intent {flg = 0x10000000 cmp = plと表示されますが、2回目以降は表示されません。 mymeal.orders/.services.NotificationDialog} from pid 1001
+0

フラグをFLAG_ACTIVITY_CLEAR_TOPに変更してください。 –

+0

本当にうまくいきました。私は活動フラグを詳しく見なければならないと思う。もう一度ありがとう。 –

+0

それがうまくいけば。私は答えとして私のコメントを移動し、それを受け入れてください:) –

答えて

2

フラグをFLAG_ACTIVITY_CLEAR_TOPに変更します。サーバーからの新しいデータには何の影響もありません。

関連する問題