简体   繁体   English

单击通知后,打开一个活动

[英]When a notification's clicked, open an activity

My code: 我的代码:

public class OnAlarmReceive extends BroadcastReceiver {

      @Override
      public void onReceive(Context context, Intent intent) 
      {
          CharSequence title = "Hello";
          CharSequence message = "la!";
          NotificationManager notificationManager;
          notificationManager = (NotificationManager) context.getSystemService("notification");
          Notification notification;
          notification = new Notification(com.example.pack.R.drawable.ic_launcher, "test", System.currentTimeMillis());

          PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, null, 0);

          notification.setLatestEventInfo(context, title, message, pendingIntent);
          notificationManager.notify(1010, notification);

      }
}

I use alarmmanager for receive, how can I open an activity when user clicks on notification? 我使用Alarmmanager进行接收,当用户单击通知时如何打开活动?

You need to change your declaration of the PendingIntent: 您需要更改PendingIntent的声明:

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, null, 0);

With something like this that would describe the intent to start the activity you want to launch (change the 3rd argument accordingly) 用类似这样的内容描述启动您要启动的活动的意图(相应地更改第三个参数)

PendingIntent pendingIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), new Intent(this, YourAppClass.class), 0);

I have this in my application for when my app receives a wake up call from alarmmanager 我的应用程序中有此消息,用于我的应用程序收到Alarmmanager的唤醒电话时

public class OnAlarmReceiver extends BroadcastReceiver {

   private static final String TAG = ComponentInfo.class.getCanonicalName(); 


@Override   
public void onReceive(Context context, Intent intent) {
    Log.d(TAG, "Received wake up from alarm manager.");

    long rowid = intent.getExtras().getLong(RemindersDbAdapter.KEY_ROWID);

    WakeReminderIntentService.acquireStaticLock(context);

    Intent i = new Intent(context, ReminderService.class); 
    i.putExtra(RemindersDbAdapter.KEY_ROWID, rowid);  
    context.startService(i);

Strings.xml Strings.xml

 <!-- Notification Bar -->
<string name="notify_new_task_message">Wake up!</string>
<string name="notify_new_task_title">Wake up! reminder</string>

Wakereminderintentservice is another java file i have to wake the phone up. Wakereminderintentservice是我必须唤醒电话的另一个Java文件。

ReminderService.java ReminderService.java

 public class ReminderService extends WakeReminderIntentService {

   public ReminderService() {
    super("ReminderService");
        }

@Override
void doReminderWork(Intent intent) {
    Log.d("ReminderService", "Doing work.");
    Long rowId = intent.getExtras().getLong(RemindersDbAdapter.KEY_ROWID);

    NotificationManager mgr =  (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(this, ReminderEditActivity.class); 
    notificationIntent.putExtra(RemindersDbAdapter.KEY_ROWID, rowId); 

    PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT); 

    Notification note=new Notification(android.R.drawable.stat_sys_warning, getString(R.string.notify_new_task_message), System.currentTimeMillis());
    note.setLatestEventInfo(this, getString(R.string.notify_new_task_title), getString(R.string.notify_new_task_message), pi);
    note.defaults |= Notification.DEFAULT_SOUND; 
    note.flags |= Notification.FLAG_AUTO_CANCEL; 

    // An issue could occur if user ever enters over 2,147,483,647 tasks. (Max int value). 
    // I highly doubt this will ever happen. But is good to note. 
    int id = (int)((long)rowId);
    mgr.notify(id, note); 


    }
}

Hope any of this code helps you. 希望以上任何代码对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM