如何使用警报管理器将数据从片段传递到广播接收器
How to Pass Data from Fragment to Broadcast Receiver with Alarm Manager
在片段内的我的应用程序中,我使用警报管理器设置了一个警报事件,警报管理器在警报事件发生时触发广播接收器Occurred.The问题是我想将数据传递给该广播接收器每次它给出 0 作为默认 value.I 已经尝试了 google 中所有可能的解决方案,但它没有
为我工作。
Fragment里面的Method如下:
public static final String EVENT = "EventData";
public void addToAlarmManager(long id) {
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(getContext().ALARM_SERVICE);
Intent eventIntent = new Intent(getContext(), TimeEventReciever.class);
eventIntent.putExtra(EVENT,id);
PendingIntent eventPendingIntent = PendingIntent.getBroadcast(getContext(), 1, eventIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, selectedTime.longValue(), eventPendingIntent);
}
TimeEventReciever class如下:
public class TimeEventReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
long eventId=intent.getLongExtra(EVENT,0);
Log.e("event","event id : "+String.valueOf(eventId));
}
}
试试这个可能对你有帮助
将您正在使用的意图中的数据作为附加项放入待定意图中。您将在 BroadCast 接收器的 onReceive 方法中获得此意图。尝试如下定义待定意图。
PendingIntent eventPendingIntent = PendingIntent.getBroadcast(activity, 0, eventIntent,PendingIntent.FLAG_CANCEL_CURRENT);
使用以下步骤使用警报管理器将数据从片段传递到广播接收器
第 1 步: 在广播接收器的意图中添加值 Class 文件。
Intent intent = new Intent(context, MyBroadcastReceiver.class);
intent.putExtra("rId", currentReminder.getId());
intent.putExtra("rTitle", currentReminder.getrTitle());
intent.putExtra("rDesc", currentReminder.getrDesc());
intent.putExtra("rDate", currentReminder.getrDate());
intent.putExtra("rTime", currentReminder.getrTime());
intent.putExtra("rCallFor", currentReminder.getrType());
intent.putExtra("rName", currentReminder.getCallLogBean().getCallName());
intent.putExtra("rMobile", currentReminder.getCallLogBean().getCallNumber());
第 2 步: 然后在以下代码的帮助下使用 Pending Intent 传递此 Intent 对象。
注意:在intent.putExtra
后立即写下下面的代码
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getActivity(), currentReminder.getId(), intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, reminderTime,
repeateInterval, pendingIntent);
注意:第 1 步和第 2 步:在您的片段或 activity 中写下您根据需要使用的片段。
============================================= ====================================
第 3 步:创建 BrodcastReceiver 文件并添加以下代码。这有助于您从广播接收器中的意图检索数据
public class MyBroadcastReceiver extends BroadcastReceiver {
public static MediaPlayer mp;
private static final int NOTIFICATION_ID = 0;
private NotificationManager mNotifyManager;
private NotificationCompat.Builder build;
String TAG="BrodcastReceiver";
int rId;
String rTitle,rDesc,rDate,rTime,rCallFor,rName,rMobile;
PrefBean prefBean=new PrefBean();
SharedPrefrenceManager sharedPrefrenceManager;
ReminderBean currentReminder;
@Override
public void onReceive(Context context, Intent intent) {
rId= intent.getIntExtra("rId",0);
rTitle= intent.getStringExtra("rTitle");
rDesc = intent.getStringExtra("rDesc");
rDate = intent.getStringExtra("rDate");
rTime = intent.getStringExtra("rTime");
rCallFor = intent.getStringExtra("rCallFor");
rName = intent.getStringExtra("rName");
rMobile = intent.getStringExtra("rMobile");
CallLogBean callLogBean=new CallLogBean();
callLogBean.setCallName(rName);
callLogBean.setCallNumber(rMobile);
Log.e(TAG, "onReceive: "+rId );
NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
mp = MediaPlayer.create(context, R.raw.alarm);
mp.start();
Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
sharedPrefrenceManager = new SharedPrefrenceManager(Const.FILE_NAME,context);
prefBean=sharedPrefrenceManager.getSharedPreferences();
String remindAs=prefBean.getShowReminderAs();
Log.e(TAG, "onReceive: "+remindAs );
if(remindAs.equals(Const.ALARAM)){
startNotification(context,intent);
}else{
startNotification(context,intent);
}
}
private void startNotification(Context context, Intent intent) {
RemoteViews notificationLayout = new RemoteViews(context.getPackageName(), R.layout.notification_small);
RemoteViews notificationLayoutExpanded = new RemoteViews(context.getPackageName(), R.layout.notification_large);
Intent notificationIntent = new Intent(context, HomeActivity.class);
notificationLayout.setTextViewText(R.id.notification_title,rTitle);
notificationLayoutExpanded.setTextViewText(R.id.not_txt_title,rTitle);
notificationLayoutExpanded.setTextViewText(R.id.not_txt_desc,rDesc);
notificationLayoutExpanded.setTextViewText(R.id.not_txt_callfor,rCallFor);
notificationLayoutExpanded.setTextViewText(R.id.not_txt_name,rName);
//notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
// | Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra("rId",rId);
notificationIntent.putExtra("rTitle",rTitle);
notificationIntent.putExtra("rDesc",rDesc);
Log.e(TAG, "startNotification:\n "+rId );
PendingIntent pendingIntent = PendingIntent.getActivity(context, rId,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
build = new NotificationCompat.Builder(context);
build.setContentTitle(rTitle)
.setContentText(rDesc)
.setChannelId(rId+"")
.setAutoCancel(true)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout)
.setCustomBigContentView(notificationLayoutExpanded)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_calendar);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(rId+"" ,
"Call Reminder",
NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("With sound");
channel.setSound(null,null);
channel.enableLights(false);
channel.setLightColor(Color.BLUE);
channel.enableVibration(true);
mNotifyManager.createNotificationChannel(channel);
}
mNotifyManager.notify(rId, build.build());
}
在片段内的我的应用程序中,我使用警报管理器设置了一个警报事件,警报管理器在警报事件发生时触发广播接收器Occurred.The问题是我想将数据传递给该广播接收器每次它给出 0 作为默认 value.I 已经尝试了 google 中所有可能的解决方案,但它没有 为我工作。 Fragment里面的Method如下:
public static final String EVENT = "EventData";
public void addToAlarmManager(long id) {
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(getContext().ALARM_SERVICE);
Intent eventIntent = new Intent(getContext(), TimeEventReciever.class);
eventIntent.putExtra(EVENT,id);
PendingIntent eventPendingIntent = PendingIntent.getBroadcast(getContext(), 1, eventIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, selectedTime.longValue(), eventPendingIntent);
}
TimeEventReciever class如下:
public class TimeEventReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
long eventId=intent.getLongExtra(EVENT,0);
Log.e("event","event id : "+String.valueOf(eventId));
}
}
试试这个可能对你有帮助
将您正在使用的意图中的数据作为附加项放入待定意图中。您将在 BroadCast 接收器的 onReceive 方法中获得此意图。尝试如下定义待定意图。
PendingIntent eventPendingIntent = PendingIntent.getBroadcast(activity, 0, eventIntent,PendingIntent.FLAG_CANCEL_CURRENT);
使用以下步骤使用警报管理器将数据从片段传递到广播接收器
第 1 步: 在广播接收器的意图中添加值 Class 文件。
Intent intent = new Intent(context, MyBroadcastReceiver.class);
intent.putExtra("rId", currentReminder.getId());
intent.putExtra("rTitle", currentReminder.getrTitle());
intent.putExtra("rDesc", currentReminder.getrDesc());
intent.putExtra("rDate", currentReminder.getrDate());
intent.putExtra("rTime", currentReminder.getrTime());
intent.putExtra("rCallFor", currentReminder.getrType());
intent.putExtra("rName", currentReminder.getCallLogBean().getCallName());
intent.putExtra("rMobile", currentReminder.getCallLogBean().getCallNumber());
第 2 步: 然后在以下代码的帮助下使用 Pending Intent 传递此 Intent 对象。
注意:在intent.putExtra
后立即写下下面的代码 PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getActivity(), currentReminder.getId(), intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, reminderTime,
repeateInterval, pendingIntent);
注意:第 1 步和第 2 步:在您的片段或 activity 中写下您根据需要使用的片段。
============================================= ====================================
第 3 步:创建 BrodcastReceiver 文件并添加以下代码。这有助于您从广播接收器中的意图检索数据
public class MyBroadcastReceiver extends BroadcastReceiver {
public static MediaPlayer mp;
private static final int NOTIFICATION_ID = 0;
private NotificationManager mNotifyManager;
private NotificationCompat.Builder build;
String TAG="BrodcastReceiver";
int rId;
String rTitle,rDesc,rDate,rTime,rCallFor,rName,rMobile;
PrefBean prefBean=new PrefBean();
SharedPrefrenceManager sharedPrefrenceManager;
ReminderBean currentReminder;
@Override
public void onReceive(Context context, Intent intent) {
rId= intent.getIntExtra("rId",0);
rTitle= intent.getStringExtra("rTitle");
rDesc = intent.getStringExtra("rDesc");
rDate = intent.getStringExtra("rDate");
rTime = intent.getStringExtra("rTime");
rCallFor = intent.getStringExtra("rCallFor");
rName = intent.getStringExtra("rName");
rMobile = intent.getStringExtra("rMobile");
CallLogBean callLogBean=new CallLogBean();
callLogBean.setCallName(rName);
callLogBean.setCallNumber(rMobile);
Log.e(TAG, "onReceive: "+rId );
NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
mp = MediaPlayer.create(context, R.raw.alarm);
mp.start();
Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
sharedPrefrenceManager = new SharedPrefrenceManager(Const.FILE_NAME,context);
prefBean=sharedPrefrenceManager.getSharedPreferences();
String remindAs=prefBean.getShowReminderAs();
Log.e(TAG, "onReceive: "+remindAs );
if(remindAs.equals(Const.ALARAM)){
startNotification(context,intent);
}else{
startNotification(context,intent);
}
}
private void startNotification(Context context, Intent intent) {
RemoteViews notificationLayout = new RemoteViews(context.getPackageName(), R.layout.notification_small);
RemoteViews notificationLayoutExpanded = new RemoteViews(context.getPackageName(), R.layout.notification_large);
Intent notificationIntent = new Intent(context, HomeActivity.class);
notificationLayout.setTextViewText(R.id.notification_title,rTitle);
notificationLayoutExpanded.setTextViewText(R.id.not_txt_title,rTitle);
notificationLayoutExpanded.setTextViewText(R.id.not_txt_desc,rDesc);
notificationLayoutExpanded.setTextViewText(R.id.not_txt_callfor,rCallFor);
notificationLayoutExpanded.setTextViewText(R.id.not_txt_name,rName);
//notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
// | Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra("rId",rId);
notificationIntent.putExtra("rTitle",rTitle);
notificationIntent.putExtra("rDesc",rDesc);
Log.e(TAG, "startNotification:\n "+rId );
PendingIntent pendingIntent = PendingIntent.getActivity(context, rId,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
build = new NotificationCompat.Builder(context);
build.setContentTitle(rTitle)
.setContentText(rDesc)
.setChannelId(rId+"")
.setAutoCancel(true)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout)
.setCustomBigContentView(notificationLayoutExpanded)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_calendar);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(rId+"" ,
"Call Reminder",
NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("With sound");
channel.setSound(null,null);
channel.enableLights(false);
channel.setLightColor(Color.BLUE);
channel.enableVibration(true);
mNotifyManager.createNotificationChannel(channel);
}
mNotifyManager.notify(rId, build.build());
}