如何在单击状态栏的通知时启动“ViewMessageListActivity”活动

时间:2012-05-01 02:39:00

标签: android

当我收到短信时,当我点击状态栏中的短信通知时,我在状态栏中看到短信通知,它没有打开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

   }

}

2 个答案:

答案 0 :(得分:1)

您需要为ViewMessageListActivity活动创建一个意图。

Intent notificationIntent = new Intent(context,ViewMessageListActivity.class);
PendingIntent contentIntent =PendingIntent.getActivity(context, 0, notificationIntent, 0);
....

并删除最后三个陈述。

答案 1 :(得分:0)

你的notificationIntent是空的,这就是问题所在。假设ViewMessageListActivity是你的尝试:

Intent notificationIntent = new Intent(context, ViewMessageListActivity.class);

这是我在Play商店中的一个应用程序中的一个更完整的示例(所以我知道它的工作原理:-D):

notification.setLatestEventInfo(NotificationService.this, getResources().getString(R.string.app_name), "Select to launch", IntentUtils.getMainActivityPendingIntent(this));

IntentUtils是我使用的辅助类,最终成为:

public static Intent getMainActivityIntent(Context context) {
    return new Intent(context, MainActivity.class);
}

public static PendingIntent getMainActivityPendingIntent(Context context) {
    return PendingIntent.getActivity(context, 0, getMainActivityIntent(context), 0);
}

这与我最初发布的内容几乎相同。