将额外的 Intent 传递给 Android 广播接收器

发布于 2024-12-19 22:20:19 字数 908 浏览 3 评论 0原文

我创建了一个 AlarmReceiver 类,用作警报的广播接收器。问题是我需要从将警报设置为广播接收器类的类发送一些值。

setAlarmManager.java

     Intent i = new Intent(mContext, AlarmReceiver.class);
     i.putExtra(KEY_ROWID, (long)taskId); 
     PendingIntent pi = PendingIntent.getBroadcast(mContext,taskId_int,i,PendingIntent.FLAG_ONE_SHOT); 
     mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);

我需要从alarmreceiver类中的intent获取KEY_ROWID。我怎样才能做到这一点? AlarmReceiver 类如下所示。

public class AlarmReceiver extends BroadcastReceiver {
          public static final String ALARM_ALERT_ACTION ="com.android.alarmclock.ALARM_ALERT";
          public static final String ALARM_INTENT_EXTRA = "intent.extra.alarm";

     @Override
      public void onReceive(Context context, Intent intent) {

             //Here I need the values from intent using bundle or anything...

    }
}

I have created a AlarmReceiver class which is used as broadcast receiver for alarm. The issue is that I need to send some values from the class which is setting the alarm to the broadcast receiver class.

setAlarmManager.java

     Intent i = new Intent(mContext, AlarmReceiver.class);
     i.putExtra(KEY_ROWID, (long)taskId); 
     PendingIntent pi = PendingIntent.getBroadcast(mContext,taskId_int,i,PendingIntent.FLAG_ONE_SHOT); 
     mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);

I need to get the KEY_ROWID from intent in alarmreceiver class. How can I do that? The AlarmReceiver class is shown below.

public class AlarmReceiver extends BroadcastReceiver {
          public static final String ALARM_ALERT_ACTION ="com.android.alarmclock.ALARM_ALERT";
          public static final String ALARM_INTENT_EXTRA = "intent.extra.alarm";

     @Override
      public void onReceive(Context context, Intent intent) {

             //Here I need the values from intent using bundle or anything...

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情洁癖 2024-12-26 22:20:19

您可以使用:

public class AlarmReceiver extends BroadcastReceiver {
          public static final String ALARM_ALERT_ACTION ="com.android.alarmclock.ALARM_ALERT";
          public static final String ALARM_INTENT_EXTRA = "intent.extra.alarm";

     @Override
      public void onReceive(Context context, Intent intent) {

             String keyid = intent.getStringExtra("KEY_ROWID");
    }
}

You can use :

public class AlarmReceiver extends BroadcastReceiver {
          public static final String ALARM_ALERT_ACTION ="com.android.alarmclock.ALARM_ALERT";
          public static final String ALARM_INTENT_EXTRA = "intent.extra.alarm";

     @Override
      public void onReceive(Context context, Intent intent) {

             String keyid = intent.getStringExtra("KEY_ROWID");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文