【发布时间】:2015-09-14 17:37:34
【问题描述】:
我正在设置一个闹钟,该闹钟每天在设备中触发一次。警报触发正常,但需要一些数据才能正常工作。该数据以这种方式发送:
AlarmManager am=(AlarmManager)SplashActivity.this.getSystemService(Context.ALARM_SERVICE);
Intent msgIntent = new Intent(SplashActivity.this, AlarmReceiver.class);
msgIntent.putExtra("todaydate", today);
PendingIntent pIntent = PendingIntent.getBroadcast(this, ALARM_REQUEST_CODE, msgIntent, Intent.FILL_IN_DATA);
am.setRepeating(AlarmManager.RTC_WAKEUP, today, AlarmManager.INTERVAL_DAY, pIntent);
然后,在 AlarmReceiver 中,在 onReceive() 内部:
public class AlarmReceiver extends BroadcastReceiver {
private DatabaseHelper dbHelper;
private int NOTIF_ALERTA_ID=666;
@Override
public void onReceive(Context context, Intent intent) {
long todayDate=intent.getLongExtra("todaydate", 0L);
if(dbHelper==null){
dbHelper=new DatabaseHelper(context);
}
Cursor c=dbHelper.getNotas(context);
if(c.moveToFirst()){
long milisTomorrow=todayDate+86400000;
do{
long noteFecha=c.getLong(1);
if(noteFecha>todayDate && noteFecha<milisTomorrow){
launchNotification(c.getString(2), context);
}
}while (c.moveToNext());
}
c.close();
}
private void launchNotification(String texto, Context ctx) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(ctx)
.setSmallIcon(android.R.drawable.stat_sys_warning)
.setLargeIcon((((BitmapDrawable) ctx.getResources()
.getDrawable(R.mipmap.ic_launcher)).getBitmap()))
.setContentTitle(ctx.getString(R.string.notenoti))
.setContentText(texto)
.setTicker("Alert!");
Intent notIntent =
new Intent(ctx, CalendarioActivity.class);
PendingIntent contIntent =
PendingIntent.getActivity(
ctx, 0, notIntent, 0);
mBuilder.setContentIntent(contIntent);
NotificationManager mNotificationManager =
(NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIF_ALERTA_ID, mBuilder.build());
}
但这返回 0。我做错了什么?
需要说明的是,我在onReceive方法中设置了一个Log条目,它被访问了。
【问题讨论】:
-
当你做 putExtra 时,今天是什么。
-
今天是否不同于 0?尝试:
msgIntent.putExtra("todaydate", 11L)例如并检查它是否正确接收到值。 -
今天有一个值,我在附加到 Intent 之前设置了另一个 Log 条目,它的值是好的。
-
将 11L 作为默认值不起作用...我得到 0。
-
您可以添加更多
AlarmReceiver代码吗?