2012-11-04 48 views
1

我有和短信應用程序,當我收到一個短信有一個對話框創建顯示消息和通知欄中創建的通知。我的問題是,當我點擊通知時,它會開始我的活動,並在原件上創建一個新的對話框,而不是隻重新調整已創建的對話框。顯示通知單擊原始對話框

NotificationManager manger = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(R.drawable.sms,smsBody, System.currentTimeMillis()); 

    Intent notificationIntent = new Intent(this, SendSMSActivity.class); 
    PendingIntent contentIntent = 
      PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT); 

    notification.setLatestEventInfo(this, ContactName,smsBody,contentIntent); 
    notification.flags = Notification.FLAG_AUTO_CANCEL; 
    //notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); 

    notification.defaults = notification.DEFAULT_SOUND | notification.DEFAULT_VIBRATE; 
    manger.notify(1, notification); 

我的活動代碼

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     //wakeup screen 
     PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE); 
     WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG"); 
     wakeLock.acquire(); 

     final String sender = getIntent().getStringExtra("PhoneNumber"); 
     final String body = getIntent().getStringExtra("smsBody"); 
     final String contactId = getIntent().getStringExtra("contactId"); 
     final String SenderName = getIntent().getStringExtra("SenderName"); 


     new ContactNameAsyncTask().execute();//this start the create dialog 

這是我的對話框代碼

私人無效ShowMessage(){

NotificationManager manger = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(R.drawable.sms,smsBody, System.currentTimeMillis()); 

    Intent notificationIntent = new Intent(this, SendSMSActivity.class); 
    PendingIntent contentIntent = 
      PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT); 

    notificationIntent.putExtra("NotificationMessage", "focused"); 
    notificationIntent.setData((Uri.parse("foobar://"+SystemClock.elapsedRealtime()))); 

    notification.setLatestEventInfo(this, ContactName,smsBody,contentIntent); 
    notification.flags = Notification.FLAG_AUTO_CANCEL; 
    //notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); 

    notification.defaults = notification.DEFAULT_SOUND | notification.DEFAULT_VIBRATE; 
    manger.notify(1, notification); 

    final Dialog dialog = new Dialog(this); 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    dialog.setContentView(R.layout.custom); 


    try { 
     TextView text = (TextView) dialog.findViewById(R.id.text); 
     text.setMovementMethod(ScrollingMovementMethod.getInstance()); 
     text.setText(smsBody); 

     TextView stext = (TextView) dialog.findViewById(R.id.sender); 
     stext.setText(ContactName); 
    }catch(NumberFormatException nfe) { 
     TextView text = (TextView) dialog.findViewById(R.id.text); 
     text.setMovementMethod(ScrollingMovementMethod.getInstance()); 
     text.setText("message not found check your inbox"); 

     TextView stext = (TextView) dialog.findViewById(R.id.sender); 
     stext.setText("unknown"); 
    } 


    ImageView image = (ImageView) dialog.findViewById(R.id.image); 
    image.setPadding(2, 2, 2, 2); 
    image.setBackgroundColor(Color.GRAY); 

    //get contact photo 
    try { 

     image.setImageURI(ContactPicUri); 
    } catch(NumberFormatException nfe) { 
     image.setImageResource(R.drawable.sender); 
     //Toast.makeText(context,"Contact image not found "+ nfe,Toast.LENGTH_LONG).show(); 
    } 

    //end get contact photo 
    //button1 
    Button dialogButtonReply = (Button) dialog.findViewById(R.id.dialogButtonReply); 
    dialogButtonReply.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      NotificationManager manger = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
      manger.cancel(1); 
       smsReply(PhoneNumber, smsBody); 
       goHome(); 
      } 
    }); 
    //button1 end 

    Button dialogButtonClose = (Button) dialog.findViewById(R.id.dialogButtonClose); 
    dialogButtonClose.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      NotificationManager manger = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
      manger.cancel(1); 
      //markMessageRead(context,PhoneNumber,smsBody); 
      goHome(); 

     } 
    }); 

    Button dialogButtonQuickReply = (Button) dialog.findViewById(R.id.dialogButtonQuickReply); 
    dialogButtonQuickReply.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      NotificationManager manger = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
      manger.cancel(1); 
      QuickReply(); 

     } 
    }); 
    dialog.show(); 

}

+0

我沒有看到任何地方,你正在創建或調用對話框。你能發佈相關的代碼嗎? –

回答

0

您應該避免使用Dialog直接根據Android docs。所以出於這個原因,我會把你指向DialogFragment。但代碼非常簡單。

FragmentTransaction ft = getFragmentManager().beginTransaction(); 
Fragment prev = getFragmentManager().findFragmentByTag("dialog"); 
if (prev != null) { 
    ft.remove(prev); 
} 
ft.addToBackStack(null); 

// Create and show the dialog. 
DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel); 
newFragment.show(ft, "dialog"); 

如果你看一看的鏈接,你會看到,並稱當你show()的片段,你通過它的標籤,在這種情況下調用它dialog。所以這確保了沒有其他對話框出現,這意味着它不會在原始對話框上顯示另一個對話框。當然,你必須擴展DialogFragment類來使你的自定義版本,但它不是很差:)