简体   繁体   English

Onclick通知,在Android中启动活动

[英]Onclick notification, start an activity in Android

I have a class for creating notifications. 我有一个用于创建通知的类。 The class is like this: 该类是这样的:

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


     }
    }

I have an activity called: QuizScreen , which is required to be opened when the notification is clicked. 我有一个活动: QuizScreen ,单击通知时需要将其打开。 I have tried using: 我试过使用:

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

But I am getting this error: 但我收到此错误:

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

Where am I going wrong? 我要去哪里错了? How should I solve this issue? 我应该如何解决这个问题?

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

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

The broadcast receiver does not count as a context? 广播接收器不算作上下文吗? Use the context parameter from the onReceive(Context context, Intent intent) instead 使用onReceive(Context context,Intent intent)中的context参数

try below code to start activity 尝试下面的代码开始活动

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

Basically as Nicklas suggested, Receiver doesnt count as context, so u ll need to use context u getting in onReceive method, or use it like context.getApplicationContext. 基本上就像Nicklas所建议的那样,Receiver并不算作上下文,因此您需要使用进入onReceive方法的上下文,或者像context.getApplicationContext一样使用它。

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

For example: 例如:

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