我有一个短信应用程序,当我收到短信时,会创建一个对话框来显示消息,并且还会在通知栏中创建一个通知。我的问题是,当我单击通知时,它会启动我的活动并在原始对话框之上创建一个新对话框,而不是仅仅重新关注已经创建的对话框。
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();
}