1

SMSを受信すると、メッセージを表示するダイアログが作成され、通知バーにも通知が作成されるSMSアプリがあります。私の問題は、通知をクリックするとアクティビティが開始され、既に作成されているダイアログに再びフォーカスするのではなく、元のダイアログの上に新しいダイアログが作成されることです。

   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

Android docsDialogに従って直接使用することは避けてください。このため、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が、それほど悪くはありません:)

于 2012-11-05T01:13:22.903 に答える