当我收到一条短信时,当我单击状态栏中的短信通知时,我在状态栏中看到短信通知,它没有打开 ViewMessageListActivity(我的对话日志)。如何打开此活动。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class SmsReceiver extends BroadcastReceiver {
static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
int notificationID = 1;
@Override
public void onReceive(Context context, Intent intent)
{
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (intent.getAction().equals(ACTION)) {
StringBuilder sb = new StringBuilder();
String from = new String();
String body = new String();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
for (Object pdu : pdus){
SmsMessage messages = SmsMessage.createFromPdu((byte[]) pdu);
sb.append(messages.getDisplayOriginatingAddress());
from = messages.getDisplayOriginatingAddress();
sb.append(messages.getDisplayMessageBody());
body= messages.getDisplayMessageBody();
//Log.i(LOG_TAG, "[SMSApp] onReceiveIntent: " + sb);
//abortBroadcast();
}// end for
}//end if
int icon = R.drawable.stat_notify_sms;
CharSequence tickerText = from + ": " + body;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
CharSequence contentTitle = "New Cybernetx Secure Text Message";
CharSequence contentText = from + " " + body;
Intent notificationIntent = new Intent();
PendingIntent contentIntent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.vibrate = new long[] { 100, 250, 100, 500};
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(notificationID, notification);
// intent.setAction(SendReceiveService.RECEIVE_SMS_ACTION);
// intent.setClass(arg0, ViewMessageListActivity.class);
// intent.putExtra("notificationID", notificationID);
// arg0.startService(intent);
// PendingIntent contentIntent =
// PendingIntent.getActivity(arg0, 0, i, 0);
/* Start the Main-Activity */
Intent i = new Intent(context, ViewMessageListActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}//end if
}
}