我有和短信应用程序,当我收到短信时,会创建一个对话框来显示消息,并在通知栏中创建通知。我的问题是,当我点击通知时,它会启动我的活动,并在原始文件的顶部创建一个新对话框,而不是仅仅重新聚焦已经创建的对话框。
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
这是我的对话框代码
private void 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)
根据Android docs,您应该避免直接使用Dialog
。因此,我会指向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
类来制作你的自定义版本,但它不是那么糟糕:)