单击通知android启动活动

时间:2014-07-11 11:03:36

标签: android android-intent notifications start-activity

点击通知后我的活动开始出现问题...

public class MyNotification extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // Toast.makeText(context, "notification", 25).show();

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
            context).setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Test Notification")
            .setContentText("This is test notification ");
    Intent myIntent = new Intent(context, MainActivity.class);
    PendingIntent intent2 = PendingIntent.getBroadcast(context, 1,
            myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notificationBuilder.setContentIntent(intent2);
    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    mNotificationManager.notify(1, notificationBuilder.build());
}

}

什么是错的?

1 个答案:

答案 0 :(得分:5)

试试这种方式

 PendingIntent intent2 = PendingIntent.getActivity(context, 1,
        myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

而不是PendingIntent.getActivity使用PendingIntent.getBroadcast

 PendingIntent intent2 = PendingIntent.getBroadcast(context, 1,
        myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

有关详细信息,请转到http://android-er.blogspot.in/2013/06/start-activity-once-notification-clicked.html