android - onclick 通知,在 Android 中启动一个 Activity

标签 android class android-intent android-activity notifications

我有一个用于创建通知的类。类是这样的:

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


     }
    }

我有一个 Activity 叫做: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);
  }
}

关于android - onclick 通知,在 Android 中启动一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14406058/

相关文章:

c# - 如何使用控件的类名或其值而不是 ID 来调用 HTML 按钮的单击函数?

android - 如何将 Bango SDK 添加到我的 android 项目中?

android - Toast 定位不起作用

android - 如何读取已连接到设备的 NFC 标签

java - ImageView 留下不需要的顶部/底部边距/填充

java - 如何修复 Android 应用程序中 CountDownTimer 的错误

java - 将文件上传到保管箱(通过同步)

java - Jackson 包装器反序列化最佳实践

javascript - 简单的 JavaScript 查询

jQuery 将类设置为变量