Reputation: 1
From main activity I call a broadcast receiver with alarm manager for start repeat function. I create also share preference for period time. How do I pass period time integer to another class broadcast receiver?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preferences = getSharedPreferences("dataiowebusb" , VATE);
String strUpdatetime = preferences.getString("Period","3");
text5.setText(strUpdatetime);
Tperiod =Integer.parseInt(strUpdatetime);
if(Tperiod>1200){
Tperiod=1200;//20min
}
sendBroadcast(new Intent(this,MyScheduleReceiver.class));//Call ala
}
public class MyScheduleReceiver extends BroadcastReceiver {
public static int period=20;
private static final long REPEAT_TIME = 1000 * period;
public void onReceive(Context context, Intent intent) {
AlarmManager service = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, MyStartServiceReceiver.class);
If I use shared preferences inside the broadcast receiver class I have errors MODE_PRIVATE..
Upvotes: 0
Views: 2408
Reputation: 13468
You can pass data on the Intent
object using the extras
attribute and its accesor method putExtra and retrieve data with getIntExtra.
So, your calling code should look like:
Intent intent=new Intent(this,MyScheduleReceiver.class)
intent.putExtra("PERIOD", Tperiod);
sendBroadcast(intent);//Call ala
To retireve it, at your receiver onReceive
method:
int tPeriod= intent.getIntExtra("PERIOD", 1200); //taking 1200 as a default value, used if no "PERIOD" Bondle is found at the Intent extras.
Upvotes: 1
Reputation: 171
When creating Intent put some data into bundle (Extras)
new Intent(this, SomeClass.class).putExtra("someKey", someValue);
When on broadcastReceiver, read data from intent onReceive method
intent.getExtras().getInt("someKey")
Upvotes: 1
Reputation: 1550
Put your values in intent extras.
Intent i = new Intent(context, MyStartServiceReceiver.class); i.putextra("key",value);
Upvotes: 0