簡體   English   中英

單擊通知后,打開一個活動

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

我的代碼:

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);

      }
}

我使用Alarmmanager進行接收,當用戶單擊通知時如何打開活動?

您需要更改PendingIntent的聲明:

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

用類似這樣的內容描述啟動您要啟動的活動的意圖(相應地更改第三個參數)

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

我的應用程序中有此消息,用於我的應用程序收到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

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

Wakereminderintentservice是我必須喚醒電話的另一個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); 


    }
}

希望以上任何代碼對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM