我如何将数据从数据库传递到广播接收器并在那里再次接收它们?
How would i pass data form database to broadcast reciever and recieve them again there?
我正在尝试开发生日提醒应用程序。我将一些数据存储为姓名、姓氏、生日日期。当用户设置生日日期时,我想将名称变量传递给 BroadcastReciever
,这样当警报通知时,我可以显示特定生日的姓名.我怎么能那样做?这是我从 activity 发送数据到 BroadcastReciever
:
的一小部分代码
if (dateSelected != null) {
Intent intent = new Intent(AddBirthday.this, AlarmReceiver.class);
intent.putExtra("extra", getFirstName);
sendBroadcast(intent);
PendingIntent pendingIntent = PendingIntent.getBroadcast(AddBirthday.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Set the alarm for a particular time.
//alarmManager.set(AlarmManager.RTC_WAKEUP, test, PendingIntent.getBroadcast(AddBirthday.this, 1 , intent, PendingIntent.FLAG_UPDATE_CURRENT));
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, test, AlarmManager.INTERVAL_DAY, pendingIntent);
}
这里是 BroadcastReciever
:
public class AlarmReceiver extends BroadcastReceiver{
private int MID;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotifyBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(
context).setSmallIcon(R.drawable.ic_launcher_icon)
.setContentTitle("Birthday Upcoming")
.setContentText("Its " + "birthday" + "Dont Forget To Buy Some Present!").setSound(alarmSound)
.setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
notificationManager.notify(MID, mNotifyBuilder.build());
MID++;
}
}
我看到了一些关于此的问题,但我不确定我该怎么做。
在你的AndroidManifest.xml
中:
<receiver android:name="com.yourapplication.AlarmReceiver">
<intent-filter>
<action android:name="com.yourapplication.CUSTOM_INTENT" />
</intent-filter>
</receiver>
准备在警报响起时触发您的 Intent 时:
Intent intent = new Intent("com.yourapplication.CUSTOM_INTENT");
intent.putExtra("extra", getFirstName);
PendingIntent pendingIntent = PendingIntent.getBroadcast(AddBirthday.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, test, AlarmManager.INTERVAL_DAY, pendingIntent);
名称存储在发送给 AlarmReceiver
的 Intent 中:
@Override
public void onReceive(Context context, Intent intent) {
String name = intent.getStringExtra("extra");
...
NotificationCompat.Builder mNotifyBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher_icon)
.setContentTitle("Birthday Upcoming for " + name)
...
我正在尝试开发生日提醒应用程序。我将一些数据存储为姓名、姓氏、生日日期。当用户设置生日日期时,我想将名称变量传递给 BroadcastReciever
,这样当警报通知时,我可以显示特定生日的姓名.我怎么能那样做?这是我从 activity 发送数据到 BroadcastReciever
:
if (dateSelected != null) {
Intent intent = new Intent(AddBirthday.this, AlarmReceiver.class);
intent.putExtra("extra", getFirstName);
sendBroadcast(intent);
PendingIntent pendingIntent = PendingIntent.getBroadcast(AddBirthday.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Set the alarm for a particular time.
//alarmManager.set(AlarmManager.RTC_WAKEUP, test, PendingIntent.getBroadcast(AddBirthday.this, 1 , intent, PendingIntent.FLAG_UPDATE_CURRENT));
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, test, AlarmManager.INTERVAL_DAY, pendingIntent);
}
这里是 BroadcastReciever
:
public class AlarmReceiver extends BroadcastReceiver{
private int MID;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotifyBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(
context).setSmallIcon(R.drawable.ic_launcher_icon)
.setContentTitle("Birthday Upcoming")
.setContentText("Its " + "birthday" + "Dont Forget To Buy Some Present!").setSound(alarmSound)
.setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
notificationManager.notify(MID, mNotifyBuilder.build());
MID++;
}
}
我看到了一些关于此的问题,但我不确定我该怎么做。
在你的AndroidManifest.xml
中:
<receiver android:name="com.yourapplication.AlarmReceiver">
<intent-filter>
<action android:name="com.yourapplication.CUSTOM_INTENT" />
</intent-filter>
</receiver>
准备在警报响起时触发您的 Intent 时:
Intent intent = new Intent("com.yourapplication.CUSTOM_INTENT");
intent.putExtra("extra", getFirstName);
PendingIntent pendingIntent = PendingIntent.getBroadcast(AddBirthday.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, test, AlarmManager.INTERVAL_DAY, pendingIntent);
名称存储在发送给 AlarmReceiver
的 Intent 中:
@Override
public void onReceive(Context context, Intent intent) {
String name = intent.getStringExtra("extra");
...
NotificationCompat.Builder mNotifyBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher_icon)
.setContentTitle("Birthday Upcoming for " + name)
...