【问题标题】:My extras are not getting to onReceive method我的演员没有得到 onReceive 方法
【发布时间】: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 代码吗?

标签: android broadcastreceiver


【解决方案1】:

尝试用下一种方式构建PendingIntent

PendingIntent pIntent = PendingIntent.getBroadcast(this, ALARM_REQUEST_CODE, msgIntent, PendingIntent.FLAG_UPDATE_CURRENT);

我将Intent.FILL_IN_DATA 替换为PendingIntent.FLAG_UPDATE_CURRENT

在检查代码之前删除并安装应用程序。

【讨论】:

  • 在将其更改为 Intent.FILL_IN_DATA 之前,我有该标志,但它无法正常工作。因为这个问题改了:stackoverflow.com/questions/5216177/…
  • FILL_IN_DATA 如何提供帮助真的很有趣。我认为在这里使用这个标志是不正确的。但是,回到你的问题。尝试将此行添加到代码 msgIntent.setAction("actionstring" + System.currentTimeMillis());
  • 我最终在接收器类中生成了 long 的值(它来自 Calendar 对象),这样代码就可以正常工作了...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多