gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:24:36 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com