Sending extras to BroadcastReceiver - android

I have an Activity that runs the following code (time and interval are defined):
Intent buzzIntent = new Intent(getBaseContext(), BuzzReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, buzzIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
buzzIntent.putExtra("interval", interval);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, interval * 60 * 1000, pendingIntent);
and a BroadcastReceiver that has the following onReceive:
#Override
public void onReceive(Context context, Intent intent) {
try {
int interval = intent.getIntExtra("interval", -1);
<... more code ...>
} catch (Exception e) {
e.printStackTrace();
}
}
but the intent.getIntExtra() returns -1 (which isn't the value of interval in the Activity, I checked), so for some reason the BroadcastReceiver isn't getting the extras that I store into the intent in the Activity.
I've tried a ton of different things but nothing seems to work. Am I missing something here?

Set flag FILL_IN_DATA while creating pending intent as below:
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, buzzIntent, Intent.FILL_IN_DATA);
You should receive extras in broadcast receiver after this change.

Have you tried calling buzzIntent.putExtra() before you pass buzzIntent to PendingIntent.getBroadcast()?

Try following code
Bundle bundle = intent.getExtras();
int interval= bundle.getInt("interval", -1);
instead of
int interval = intent.getIntExtra("interval", -1);

Related

Check if an alarm manager is running (not working correctly)

first I did read other questions similar to this one, the one that seemed most helpful is this one, and tried using the top two answers to fix my issue, I have two alarm managers in main activity as shown here
private static final int NOTIFICATION_REMINDER_PROMPT = 0;
private static final int NOTIFICATION_REMINDER_UPDATE = 1;
if(!alarmIsRunning(this, new Intent(this, PromptReceiver.class))) {
Intent notifyIntent = new Intent(this, PromptReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast
(this, NOTIFICATION_REMINDER_PROMPT, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
1000 * 60 * 60, pendingIntent);
}
if(!alarmIsRunning(this, new Intent(this, UpdateReceiver.class))) {
Intent notifyIntent2 = new Intent(this, UpdateReceiver.class);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast
(this, NOTIFICATION_REMINDER_UPDATE, notifyIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager2 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager2.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
1000 * 60 * 60 * 50, pendingIntent2);
}
also the code for function that is used in the if function is as follows
public static boolean alarmIsRunning(Context context, Intent intent){
return PendingIntent.getBroadcast(context, 0,
intent, PendingIntent.FLAG_NO_CREATE) != null;
}
Now my issue is that when I debug, the first if condition always gives true, and the second always gives false and runs the code inside, but when I went for the second way which is using the adb shell dumpsys alarm line in my terminal, only the second one is working. There is something that seems to be I don't understand about this.
When you fetch a pending intent to see if it exists, you need to recreate it exactly as you did before. So you'll need to pass the original request code to the alarmIsRunning() method.
Here is an extract from one of the methods I use for reference:
private fun doesPendingIntentExist(requestCode: Int, intent: Intent): Boolean {
val tempPendingIntent = PendingIntent.getBroadcast(context,
requestCode, intent,
PendingIntent.FLAG_NO_CREATE)
tempPendingIntent?.cancel()
return tempPendingIntent != null
}
TL;DR: You need to pass the original request code to the PendingIntent.getBroadcast() method.

I use "Action to make Phone Call" but I need to make a called automatically for 5 or 10 min

This is an example
But I need to make a called for 5 minutes or 10 minute automatically. How I can make it?
Intent in=new Intent(Intent.ACTION_CALL,Uri.parse("0000000000"))
try{
startActivity(in);
}
you can use broadcast receiver and set action as Time change.And set Alarm for every 5 min to send action to broadcast receiver.
Add this code in your activity:
PendingIntent service = null;
Intent intentForService = new Intent(context.getApplicationContext(), YourService.class);
final AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
final Calendar time = Calendar.getInstance();
time.set(Calendar.MINUTE, 0);
time.set(Calendar.SECOND, 0);
time.set(Calendar.MILLISECOND, 0);
if (service == null) {
service = PendingIntent.getService(context, 0,
intentForService, PendingIntent.FLAG_CANCEL_CURRENT);
}
alarmManager.setRepeating(AlarmManager.RTC, time.getTime()
.getTime(), 60000, service);
Create Broadcast Receiver and add code for Make Call in onReceive() of receiver.
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("BroadcastReceiver", "debut receive");
Intent in=new Intent(Intent.ACTION_CALL,Uri.parse("0000000000"))
startActivity(in);
}
}
I hope its helpful to you.
Happy Coding..

intent.putExtra doesn´t deliver newest values of a string to intent

I use AlarmManager and try to give some values in putExtra to my BroadcastReceiver. The values I send go to the BroadcastReceiver, it works fine to transmit values.
But I send my variable "counter" and I always get the old values that existed on the first start of my setRepeating(). And I know that the counter values are ways more high that I see there. So when the values change nothing happens. How can I have an event every half hour with right values?!
I've searched now for 3 hours but can't find a solution to make an interaction of my AlarmManager and some values out of a Sensor...
public void startAlarm(View view) {
try {
AlarmManager alarms = (AlarmManager) this
.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(),
MyAlarmReceiver.class);
intent.putExtra("startStepCounter", startStepCounter);
intent.putExtra("lastStepCounter", lastStepCounter);
final PendingIntent pIntent = PendingIntent.getBroadcast(this,
1234567, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarms.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), timeToAlarmMilli, pIntent);
} catch (Exception e) {
}
}
public class MyAlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("Alarm Receiver", "Entered");
//
Bundle bundle = intent.getExtras();
int local_start = bundle.getInt("startStepCounter");
int local_last = bundle.getInt("lastStepCounter");
Toast.makeText(context,
"ALARM " + local_start + " " + local_last,
Toast.LENGTH_SHORT).show();
}
}
look at this part of your code
final PendingIntent pIntent = PendingIntent.getBroadcast(this,
1234567, intent, PendingIntent.FLAG_CANCEL_CURRENT);
you need to provide uniuque id for secound part each time you use pending intent, so instead of 1234567, use a unique id.

Setting 2 alarms with one receiver

I need to trigger two alarms. The first one fires correctly if its just one alarm. If I include the code for the second one, the alarms overlap and the desired functionality is not achieved. My question is do I need two broadcast receivers or I can do it with one?
first alarm:
public void triggerEnable(boolean enableData, int hourInDay, int minInDay) {
Calendar calendar = Calendar.getInstance();
//if (enableData) {
calendar.set(Calendar.HOUR_OF_DAY, hourInDay);
calendar.set(Calendar.MINUTE, minInDay);
// } else {
// calendar.set(Calendar.HOUR_OF_DAY, hourInDay);
// calendar.set(Calendar.MINUTE, minInDay);
// }
calendar.set(Calendar.SECOND, 0);
Intent broadcastIntent = new Intent("com.sang.mobiledata.IntentAction.RECEIVE_CONN_UPDATE");
broadcastIntent.putExtra("FLAG_KEY", enableData);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi);
}
broadcast receiver:
#Override
public void onReceive(Context context, Intent intent) {
if(CONN_ACTION.equals(intent.getAction())) {
boolean enableConn = intent.getBooleanExtra("FLAG_KEY", false);
objNetwork.setMobileDataEnabled(context, enableConn);
//what do I do here to have different values for the second alarm?
}
}
}
This can be achieved by using single broadcastreceiver but using pending intents with different ids.
Right now what you are doing is sending multiple pending intents with same ID. Because of this they are overlapping. What you need to do is send another pending intent with a different ID.
First intent
PendingIntent pi = PendingIntent.getBroadcast(this, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Second intent
PendingIntent pi = PendingIntent.getBroadcast(this, 1, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Then with the help of alarmManager, fire these pending intents to be received by your broadcastreceiver.
Hope it helps
As Stated in android doc.
If there is already an alarm scheduled for the same IntentSender, it will first be canceled.
I believe you need to set the timing one by one.
You have to create two pending Intents.
Intent alarm1 = new Intent("com.sang.mobiledata.IntentAction.RECEIVE_CONN_UPDATE");
broadcastIntent.putExtra("FLAG_KEY", enableData);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, alarm1 , PendingIntent.FLAG_UPDATE_CURRENT);
Intent alaram2 = new Intent("com.sang.mobiledata.IntentAction.RECEIVE_CONN_UPDATE");
broadcastIntent.putExtra("FLAG_KEY", enableData);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, alaram2 , PendingIntent.FLAG_UPDATE_CURRENT);
Then you have to register two broadcast receivers.
private static BroadcastReceiver alarm1= null;
private static BroadcastReceiver alaram2= null;
alarm1= new BroadcastReceiver() {
#Override
public void onReceive(Context arg0, Intent arg1) {
if(CONN_ACTION.equals(intent.getAction())) {
boolean enableConn = intent.getBooleanExtra("FLAG_KEY", false);
objNetwork.setMobileDataEnabled(context, enableConn);
}
}
alaram2=new BroadcastReceiver(){
#Override
public void onReceive(Context arg0, Intent arg1) {
//put your code for the second alarm here
}
Try this. I am sure this will work.

intent for BroadcastReceiver does not work correctly

How can I 'clean' values from extra intent?
Activity:
#Override
public void onCreate(Bundle savedInstanceState) {
...
Intent intent = new Intent(this, MyBroadcastReceiver .class);
intent.putExtra("valueOne", "valOne");
intent.putExtra("valueTwo", true);
intent.putExtra("valueThree", 1);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (5 * 1000), pendingIntent);
...
}
BroadcastReceiver:
#Override
public void onReceive(Context context, Intent intent) {
...
String valueOne= intent.getStringExtra("valueOne");
Boolean valueTwo= intent.getBooleanExtra("valueTwo", false);
Integer valueThree= intent.getIntExtra("valueThree", 0);
// Log.i("info", valueOne) >> valOne
// Log.i("info", valueTwo.toString()) >> false
// Log.i("info", valueThree.toString()) >> 1
...
}
If I change value in Activity and run application again, I get same values like in first start. I try delete app from my phone/virtual machine, clean project, but problem stay :(
Anybody help me?
In the Pending Intent declaration try to set the following flag:
PendingIntent.FLAG_UPDATE_CURRENT
In your case it should be the following:
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, PendingIntent.FLAG_UPDATE_CURRENT);
First, i suggest you to format your code here. So that people here will be glad to read your problem code. Your code list above does NOT register any BroadcastReceiver to the system. You'd better to check out ApiDemo for more details.
And Also See this one

Categories

Resources