在我的应用程序中,我正在使用警报管理器实现 android 通知,以在特定时间获取通知。我在同一时间成功收到通知,但问题是当我的应用程序处于后台时(通过单击主页按钮),如果发生通知并且用户单击通知,那么它会显示强制关闭错误。
我正在设置这样的通知
public void setRepeatingAlarm(long tym)
{
Intent intent = new Intent(this, ReminderService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ tym, 86400000, pendingIntent);
}
and this is my broadcast receiver class
public class ReminderService extends BroadcastReceiver {
NotificationManager nm;
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent)
{
Intent intent_view = new Intent(Intent.ACTION_MAIN);
intent_view.setClass(context.getApplicationContext(), CoachAppActivity.class);
intent_view.setAction("android.intent.action.MAIN");
intent_view.addCategory("android.intent.category.LAUNCHER");
intent_view.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "Coach App";
CharSequence message = "View your daily tips.";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
intent_view, 0);
Notification notif = new Notification(R.drawable.icon,"View your daily tips.", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
notif.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(1, notif);
}
}
在上述代码的帮助下,我收到了用户想要的特定时间的通知。现在我正在更新我的活动类的 onResume() 方法上的 UI 元素,如下所示
public void updateUi(List<ParseObject> objects)
{
txtData = (TextView) findViewById(R.id.textView1);
txt_link1 = (TextView) findViewById(R.id.txt_link1);
txt_link2 = (TextView) findViewById(R.id.txt_link2);
txtData.setText(objects.get(0).getString("DailyInfo"));
SpannableString content1 = new SpannableString(objects.get(0)
.getString("Title_Hyperlink1"));
content1.setSpan(new UnderlineSpan(), 0, content1.length(), 0);
txt_link1.setText(content1);
SpannableString content2 = new SpannableString(objects.get(0)
.getString("Title_Hyperlink2"));
content2.setSpan(new UnderlineSpan(), 0, content2.length(), 0);
txt_link2.setText(content2);
link1 = (objects.get(0).getString("Hyperlink1").toString() == null ? ""
: objects.get(0).getString("Hyperlink1").toString());
link2 = (objects.get(0).getString("Hyperlink2").toString() == null ? ""
: objects.get(0).getString("Hyperlink2").toString());
}
当我调试应用程序时,我发现我无法获取 UI 元素的值。如果我通常在不单击通知的情况下启动应用程序,它不会产生任何问题并且应用程序会进入前台。在这种情况下请帮助我,如果有任何混淆,请告诉我。