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();

}

4

1 回答 1

0

您应该避免Dialog按照 Android文档直接使用。因此,出于这个原因,我将向您指出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。因此,这可以确保没有其他 Dialog 正在显示,这意味着它不会在原始 Dialog 之上显示另一个。当然,您必须扩展DialogFragment该类以制作您的自定义版本,但它还不错:)

于 2012-11-05T01:13:22.903 回答