【问题标题】:How to Pass Data from Fragment to Broadcast Receiver with Alarm Manager如何使用警报管理器将数据从片段传递到广播接收器
【发布时间】:2020-06-10 01:20:47
【问题描述】:

在片段内的我的应用程序中,我使用警报管理器设置了警报事件,当警报事件发生时触发广播接收器。问题是我想将数据传递给该广播接收器,每次它给出 0 作为默认值。我已经尝试了谷歌的所有可能的解决方案,但它没有 为我工作。 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 类如下:

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));
}
}

【问题讨论】:

    标签: android android-intent broadcastreceiver alarmmanager android-pendingintent


    【解决方案1】:

    试试这个可能对你有帮助

    将您正在使用的意图中的数据放入待处理的意图中作为附加。您将在广播接收器的 onReceive 方法中获得此意图。尝试如下定义待定意图。

    PendingIntent eventPendingIntent = PendingIntent.getBroadcast(activity, 0, eventIntent,PendingIntent.FLAG_CANCEL_CURRENT);
    

    【讨论】:

      【解决方案2】:

      使用下面的步骤通过警报管理器将数据从片段传递到广播接收器

      第 1 步: 在广播接收器类文件的意图中添加值。

         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 步:写下您根据要求使用的片段或活动。

      ================================================ ====================================

      第 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());
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-31
        相关资源
        最近更新 更多