OBX
OBX

Reputation: 6114

Error calling getIntent() from Broadcast receiver

I am trying to send an integer value from an Activity to a Broadcast receiver, Here is the method code in my Activity:

public void vibrator(View view){
    if (((ToggleButton) view).isChecked()) {

        Toast.makeText(getApplicationContext(), "Vibration ON",
                Toast.LENGTH_LONG).show();

        Intent intent = new Intent(AddAlarm.this, AlarmReceiver.class);
        intent.putExtra("vibrator",1);

    }
}

And now , from the Broadcast Receiver, when I try to invoke :

Intent receive = getIntent();

This is an error, but how to do this in the correct way ?

The code inside AlarmReceiver:

public class AlarmReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(final Context context, Intent intent) {
    int vibrator = intent.getIntExtra("vibrator", 1);

    PendingIntent sender = PendingIntent.getBroadcast(context, 0, 
intent, 0);


    Uri alarmUri = 
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    if (alarmUri == null)
    {
        alarmUri = 
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    }
    Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
    ringtone.play();


    //intent to call the activity which shows on ringing
    Intent myIntent = new Intent(context, Time_Date.class);
    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(myIntent);

    //display that alarm is ringing
    Toast.makeText(context, "Alarm Ringing...!!!", 
Toast.LENGTH_LONG).show();

    ComponentName comp = new ComponentName(context.getPackageName(),
            AlarmService.class.getName());
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);

    if(vibrator==1)
    {
        Log.w("the value I get is","given as"+vibrator);

    }


}

}

Upvotes: 0

Views: 773

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

Error calling getIntent() from Broadcast receiver

getIntent() method is not from BroadcastReceiver class.

In BroadcastReceiver to get intent which is send using sendBroadcast method use onReceive method second parameter which is Intent :

 @Override
   public void onReceive(Context context, Intent intent) {
       int vibrator=intent.getIntExtra("vibrator",1);
   }

Upvotes: 3

Related Questions