0

我创建了一个通知应用程序。通知即将到来。我想通过单击通知进入一个新活动。我不知道该怎么做。请给我帮助。谢谢!

我有 2 个通知类 1. Notification_2Activity

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
       DatePicker dp = (DatePicker) findViewById(R.id.datePicker1);
        TimePicker tp = (TimePicker) findViewById(R.id.timePicker1);
     int month = dp.getMonth();
        int year = dp.getYear();
        int dayofmonth= dp.getDayOfMonth();

        int hourofday=tp.getCurrentHour();
        int minute=tp.getCurrentMinute();
        EditText e = (EditText) findViewById(R.id.editText1);
      e.setText("montho="+(month+1)+" year="+year+" day="+dayofmonth+" hour"+hourofday+" minute="+minute);
 Calendar cal = Calendar.getInstance();       //for using this you need to import java.util.Calendar;

        // add minutes to the calendar object
        cal.set(Calendar.MONTH,month);

        cal.set(Calendar.YEAR,year);                
        cal.set(Calendar.DAY_OF_MONTH, dayofmonth);
        cal.set(Calendar.HOUR_OF_DAY, hourofday);
        cal.set(Calendar.MINUTE, minute);


        Intent alarmintent = new Intent(getApplicationContext(), AlarmReceiver.class);
        alarmintent.putExtra("title","Title of our Notification");
        alarmintent.putExtra("note","Description of our  Notification");
        PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), HELLO_ID,
                alarmintent,PendingIntent.FLAG_UPDATE_CURRENT|  Intent.FILL_IN_DATA);


        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

这是我的第二类 Alarmreceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub


    NotificationManager manger = (NotificationManager)     
            context.getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notification = new Notification(R.drawable.ic_launcher, "Combi Note",
             System.currentTimeMillis());
            PendingIntent contentIntent = PendingIntent.getActivity(context, 
            NOTIFICATION_ID, 
            new Intent(context, NotifyMessage.class), 0);

                    Bundle extras=intent.getExtras();
                    String title=extras.getString("title");
            //here we get the title and description of our Notification
                        //
                    String note=extras.getString("note");
                    notification.setLatestEventInfo(context, note, title, contentIntent);
                    notification.flags = Notification.FLAG_INSISTENT;
                    notification.defaults |= Notification.DEFAULT_SOUND;
            //here we set the default sound for our 
            //notification

                    // The PendingIntent to launch our activity if the user selects this notification
                    manger.notify(NOTIFICATION_ID++, notification);


}
4

3 回答 3

2
Intent notificationIntent = new Intent(this, youractivity.class);
PendingIntent contentIntent = PendingIntent
                                   .getActivity(this, 0, notificationIntent, 0);

在清单文件中添加您的活动名称

于 2012-04-16T09:14:36.633 回答
1

pendingIntent在您的通知中添加一个并在本地广播。

Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
...
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());
于 2016-01-24T08:03:35.860 回答
0

我在我的情况下使用它:

public void createNotificationToClient(Context context, String payload)
    NotificationManager notificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.icon,
                    "title", System.currentTimeMillis());
            // Hide the notification after its selected
     notification.flags |= Notification.FLAG_AUTO_CANCEL;
       Intent intent = new Intent(context, yourActivity.class);
       intent.putExtra("payload", payload);

       intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
       PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                        intent, 0);
       notification.setLatestEventInfo(context, "title", payload,
                         pendingIntent);
       notificationManager.notify(0, notification);
}
于 2012-04-16T09:20:40.047 回答