-3

I'm sending data from one class using this

 intent.putExtra("alarmMgr_id", alarm_id);

I'm getting the data in the other class in the bundle but I don't know how to retrieve it from the bundle im attaching a screenshot:

enter image description here

Please guide me how to get this data that has red star at its end; I want to store these two variables wakelockid=3, alarmMgr_id=9 as integers.

I did already try this but it didn't worked for me so please see the debug mode screenshot; the alarmManager_id returns null every time.

 Bundle extras = intent.getExtras(); 
 int alarmManager_id = extras.getInt("alarmMgr_id");

Consider this as class A that sends the data

public class AlarmBroadcastReciever extends WakefulBroadcastReceiver {
// The app's AlarmManager, which provides access to the system alarm
// services.
private AlarmManager alarmMgr;
// The pending intent that is triggered when the alarm fires.
private PendingIntent alarmIntent;

@Override
public void onReceive(Context context, Intent intent) {
    String alarm_id = intent.getAction();
    Intent service = new Intent(context, AlarmSchedulingService.class);
    service.putExtra("alarmMgr_id", alarm_id);
    // Start the service, keeping the device awake while it is launching.
    startWakefulService(context, service);
}


@SuppressLint("NewApi")
public void setAlarm_RepeatOnce(Context context, int year, int month,
        int date, int hours, int minutes, TaskModel currentTask) {
    try {
        alarmMgr = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);

        int alarm_id = currentTask._id;
        Intent intent = new Intent(context, AlarmBroadcastReciever.class);
        intent.setAction(String.valueOf(alarm_id));  

        alarmIntent = PendingIntent.getBroadcast(context, alarm_id , intent,  
                  PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.DATE, date);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.HOUR_OF_DAY, hours);
        calendar.set(Calendar.MINUTE, minutes);

        alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                alarmIntent);

        // Enable {@code AlarmBootBroadCastReceiver} to automatically
        // restart the alarm
        // when the
        // device is rebooted.
        ComponentName receiver = new ComponentName(context,
                AlarmBootBroadcastReceiverRepeatOnce.class);
        PackageManager pm = context.getPackageManager();

        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
    } catch (Exception ex) {
        String e = ex.getLocalizedMessage();
    }
}
}

and this as class B which recieves the data now if you go to the screenshot you can see im getting the data in the bundle but I don't know how to extract it and you can also see in the code the methods I have tried to extract the values but all return null.

public class AlarmSchedulingService extends IntentService {
DatabaseHelper db;
Context mContext;
public AlarmSchedulingService() {
    super("SchedulingService");
}

// An ID used to post the notification.
public int NOTIFICATION_ID;//= 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
//Bundle extras;


@SuppressLint("NewApi") @Override
protected void onHandleIntent(Intent intent) {
    try{


       // Get passed values
    Bundle extras = intent.getExtras();
    int uniqueID = extras.getInt("alarm_id");
    int alarmMgr_id =  extras.getInt("alarmMgr_id");  
    int alarmMgr_idd = extras.getParcelable("alarmMgr_id");



   // NOTIFICATION_ID = alarmMgr_id;//uniqueID;
   // sendNotification(alarmMgr_id);
    // Release the wake lock provided by the BroadcastReceiver.
    AlarmBroadcastReciever.completeWakefulIntent(intent);
    }
    catch(Exception e)
    {
        String ex = e.getLocalizedMessage();
    }
}
// Post a notification
private void sendNotification(  int alarm_id) {
    //code for notificaiton generation
}
} 
halfer
  • 19,824
  • 17
  • 99
  • 186
mushahid
  • 504
  • 5
  • 21

1 Answers1

1

You can do this way in onCreate of your Activity

  int alarmMgr_id;

  if(getIntent().getIntExtra("alarmMgr_id",yourDefaultValue)!=null)
  alarmMgr_id=getIntent().getIntExtra("alarmMgr_id",yourDefaultValue);

But after seeing you code, I realised that here in this line of code

service.putExtra("alarmMgr_id", alarm_id);

you are sending alarm_id which is a String variable and in you service where you are trying to retrieve that like this

 int alarmMgr_id =  extras.getInt("alarmMgr_id");  

i.e you are using getInt(), So just change your this line to this

 int alarmMgr_id =  Integer.parseInt(extras.getString("alarmMgr_id"));  

and this will do the trick.

Mukesh Rana
  • 4,051
  • 3
  • 27
  • 39