0

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

   }

}

4

2 回答 2

1

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

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

并删除最后三个语句。

于 2012-05-01T02:47:49.890 回答
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);
}

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

于 2012-05-01T02:51:51.190 回答