我有一个闹钟接收器,每天在特定时间每天在mainActivity中运行:
public void scheduleAlarm()
{
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 16);
calendar.set(Calendar.MINUTE, 25);
Intent intentAlarm = new Intent(this, AlarmReceiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), PendingIntent.getBroadcast(this,1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
}
在另一个名为alarmReceiver的类中我尝试更改textView上下文但它崩溃了......
public class AlarmReceiver extends BroadcastReceiver {
NotificationCompat.Builder mBuilder;
@Override
public void onReceive(Context context, Intent intent) {
TextView black;
black = (TextView) ((Activity)context).findViewById(R.id.BlackCounterDisplay);
black.setText("toto");
}
}
无法找到原因......
答案 0 :(得分:0)
如果您look at your stack trace,则会看到ClassCastException
,因为传递给Context
的{{1}}不是onReceive()
。
答案 1 :(得分:0)
你创建了你应该在主要活动中检查的全局变量 并在onReceive()中将其设置为true 然后在主要活动上更改文本 但是,如果你想要一个警报或期间警报,那就更好了