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
Related
I am making an alarm, and service is started by the alarm. I want services to receive Extra data so with googling I set the flag as Intent.FILL_IN_DATA. Below is my code.(Only important part is shown)
(I have several alarm mode, and different services are called)
public static void setAlarm(Context mContext, int mode){
if(mode==A){
intent = new Intent(mContext, AService.class);
}
else{
intent = new Intent(mContext, BService.class);
intent.putExtra(NOTI_MODE, mode);
}
PendingIntent pIntent = PendingIntent.getService(mContext, 0, intent,Intent.FILL_IN_DATA);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
Log.d("set alarm","alarm 4.4");
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
} else {
Log.d("set alarm","alarm 4.4 under");
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
}
}
public static void cancelAlarm(Context mContext){
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
PendingIntent pIntent = PendingIntent.getService(mContext, 0, new Intent(mContext, AService.class),Intent.FILL_IN_DATA);
alarmManager.cancel(pIntent);
pIntent = PendingIntent.getService(mContext, 0, new Intent(mContext, BService.class),Intent.FILL_IN_DATA);
alarmManager.cancel(pIntent);
}
service side code(edit: this code is in BService class. AService does not need to receive Extras.)
public int onStartCommand(Intent intent, int flags, int startId) {
int notiMode=intent.getIntExtra(AlarmManagerHelper.NOTI_MODE, -1);
}
However, the result is always -1, which means no Extras is being received.
What is the problem with my code? I'm googling for hours and no hope.
Please help if you have any ideas.
Thanks in advance.
EDIT: setAlarm method is in
public class AlarmManagerHelper extends BroadcastReceiver{} class.
and below is from AndroidManifest
<receiver android:name="com.example.test.AlarmManagerHelper" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
When I use
pIntent = PendingIntent.getService(mContext, 0, new Intent(mContext, BService.class), 0);
instead of
pIntent = PendingIntent.getService(mContext, 0, new Intent(mContext, BService.class),Intent.FILL_IN_DATA);
at cancelAlarm(), service gets Extras all right (but the alarm does not cancel of course)
I don't get why setAlarm() does not work when I change cancelAlarm()..
I found the solution. Quite embarrassing.. I should have searched more for myself before posting the question...
Changed Intent.FILL_IN_DATA to PendingIntent.FLAG_UPDATE_CURRENT and now it works like charm.
Does not know why, but if you are suffering from same problem, please try this solution :)
According to the code you posted, you have two services (AService and BService).
I am guessing that the code snippet you are sharing is for your AService?
I think the top part of your setAlarm method should be:
public static void setAlarm(Context mContext, int mode)
{
if(mode==A)
{
intent = new Intent(mContext, AService.class);
// NOTE: Add this line!
intent.putExtra(NOTI_MODE, mode);
}
else
{
intent = new Intent(mContext, BService.class);
intent.putExtra(NOTI_MODE, mode);
}
// snipped rest of code in this method
}
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.
first of all , i would like to apologize for my English its bad
all of alarm that i create by this class
Intent intent = new Intent(SETALARM.this, ALARMRECEIVER.class);
intent.putExtra("pk", pk);
sender = PendingIntent.getBroadcast(this, pk, intent, PendingIntent.FLAG_CANCEL_CURRENT);
am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),60000, sender);
were cleared when device is shut off
what should i do to restore all of alarm back
thank you very much for your help
edit
here is receiver class
#Override
public void onReceive(Context context, Intent intent) {
WakeLocker.acquire(context);
pk = Integer.parseInt(intent.getExtras().get("pk").toString());
Intent intent2 = new Intent(context,ALERT.class);
intent2.putExtra("pk", pk);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent2);
WakeLocker.release();
}}
If you mean that you lose the alarms when the device is turned off then this issue has been addressed well here
https://stackoverflow.com/a/5439320/374866
I have the following code for a one off alarm...
public boolean onMenuItemClick(MenuItem item) {
Intent intent = new Intent(CalendarViewActivity.this, OneShotAlarm.class);
Bundle bun = new Bundle();
bun.putString("data", "hello this is my message...");
intent.putExtras(bun);
PendingIntent sender = PendingIntent.getBroadcast(CalendarViewActivity.this,
0, intent, 0);
// We want the alarm to go off 5 seconds from now. TODO
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 5);
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
return false;
}
});
}
public class OneShotAlarm extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO
Toast.makeText(context, "Alarm! " + intent.getExtras().getString("data"), Toast.LENGTH_SHORT).show();
}
}
The alarm correctly goes off, but the "data" extra isn't getting retrieved for some reason, and is being set to null.
Thanks for the help!
Try:
Intent intent = new Intent(CalendarViewActivity.this, OneShotAlarm.class);
intent.putExtra("data", "hello this is my message...");
PendingIntent sender = PendingIntent.getBroadcast(CalendarViewActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Possible duplicate of getExtra from Intent launched from a pendingIntent
If that doesn't help, please tell us a bit more about your files (you need to have an activity where you set up the alarm, a broadcastreceiver that starts a service, and the service where you tell the app what to do when the alarm goes off)
Citation from the doc for putExtra function:
name The name of the extra data, with package prefix.
Couldn't it be the reason?
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);