簡體   English   中英

Onclick通知,在Android中啟動活動

[英]Onclick notification, start an activity in Android

我有一個用於創建通知的類。 該類是這樣的:

public class TimeAlarm extends BroadcastReceiver {

     NotificationManager nm;

     @Override
     public void onReceive(Context context, Intent intent) {
      nm = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);
      CharSequence from = "Scheduled Training";
      CharSequence message = "Please attend the scheduled training";
      //Intent notifyIntent = new Intent();



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

      Notification notif = new Notification(R.drawable.ic_launcher,
        "NeuroQ Scheduled Training", System.currentTimeMillis());
      notif.setLatestEventInfo(context, from, message, contentIntent);
      nm.notify(1, notif);


     }
    }

我有一個活動: QuizScreen ,單擊通知時需要將其打開。 我試過使用:

Intent quiz_intent = new Intent(TimeAlarm.this, QuizScreen.class);
                        TimeAlarm.this.startActivity(quiz_intent);

但我收到此錯誤:

The constructor Intent(TimeAlarm, Class<QuizScreen>) is undefined

我要去哪里錯了? 我應該如何解決這個問題?

使用Intent quiz_intent = new Intent(context, QuizScreen.class); 代替Intent quiz_intent = new Intent(TimeAlarm.this, QuizScreen.class); 例如,

public class MyBroadcastReceiver extends BroadcastReceiver {
  private NotificationManager mNotificationManager;
  private int SIMPLE_NOTFICATION_ID;

  @Override
  public void onReceive(Context context, Intent intent) {
    mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notifyDetails = new Notification(R.drawable.android,"Time Reset!",System.currentTimeMillis());
    PendingIntent myIntent = PendingIntent.getActivity(context, 0, new Intent(context, Second.class), 0);
    notifyDetails.setLatestEventInfo(context, "Time changed", "Click on me to view my second activity", myIntent);
    notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL;

    mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
  }
}

廣播接收器不算作上下文嗎? 使用onReceive(Context context,Intent intent)中的context參數

嘗試下面的代碼開始活動

Intent quiz_intent = new Intent(context, QuizScreen.class);
                        TimeAlarm.this.startActivity(quiz_intent);

基本上就像Nicklas所建議的那樣,Receiver並不算作上下文,因此您需要使用進入onReceive方法的上下文,或者像context.getApplicationContext一樣使用它。

使用: Intent quiz_intent = new Intent(context, QuizScreen.class); 代替Intent quiz_intent = new Intent(TimeAlarm.this, QuizScreen.class);

例如:

public class MyBroadcastReceiver extends BroadcastReceiver {

private NotificationManager mNotificationManager;
private int SIMPLE_NOTFICATION_ID;

@Override
public void onReceive(Context context, Intent intent) {

    mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notifyDetails = new Notification(R.drawable.android,"Time Reset!",System.currentTimeMillis());
    PendingIntent myIntent = PendingIntent.getActivity(context, 0, new Intent(context, Second.class), 0);
    notifyDetails.setLatestEventInfo(context, "Time changed", "Click on me to view my second activity", myIntent);
    notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL;

    mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);`

暫無
暫無

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

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