I have an app that schedules tweets at either given times or at set intervals. I use a repeating broadcastIntent to send the tweets. I include extras for the message, username, etc. I use the md5sum of the username+message as the unique identifier so that each intent is unique. The code works fine for one tweet, but when I add another, the new tweet contains the same extras as the first one.
private void setupTimedTweet(int position, Context c, String username,
String message, String day, String mentions, String timeValue){
Bundle b = new Bundle();
b.putString("username", username);
Toast.makeText(c, "New tweet saved, "+ message, Toast.LENGTH_LONG).show();
b.putString("message", message);
b.putString("mentions", mentions);
b.putString("day", day);
Intent myIntent = new Intent(c, TweezeeReceiver.class);
myIntent.putExtras(b);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(c, md5(username+message), myIntent, 0);
AlarmManager alarmManager =
(AlarmManager)c.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.setTimeZone(TimeZone.getDefault());
calendar.set(Calendar.HOUR_OF_DAY,Integer.parseInt(timeValue.split(":")[0]));
calendar.set(Calendar.MINUTE, Integer.parseInt(timeValue.split(":")[1]));
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent;
}
The toast in that method has the right info. When setting the new alarm, there are no issues. However the receiver never gets the updated extras for the new tweets. If I schedule a tweet, "test1", and then another, "test2", the setupIntervalTweet() method has the right info, but when the receiver receives it, it has the same info from the "test1" tweet.
@Override
public void onReceive(Context c, Intent i) {
Log.d("RECERIVER", "TWEET RECIEVED!");
Bundle tweetInfo = i.getExtras();
message = tweetInfo.getString("message");
username = tweetInfo.getString("username");
day = tweetInfo.getString("day");
prefs = PreferenceManager.getDefaultSharedPreferences(c);
ctx = c;
mHandler = new Handler();
sendTweet();
}
Why is my receiver not getting the correct extras?
Full source can be found here. https://github.com/T3hh4xx0r/TweeZee/
EDIT:
Ive got it working for updating the tweets, but I am unable to get the new extras to work on a new tweet to save with new extras. I'm using the FLAG_UPDATE_CURRENT
to update my pending intent. Ive heard to use, FLAG_CANCEL_CURRENT
to start the new one, but I don't want to cancel one, I just want to start a new one. I haven't been able to find a flag for new or anything similar.
Here's the updated code.
private void setupIntervalTweet(int position, Context c, String username,
String message, String wait, String day,
String mentions, boolean updating, String og) {
Bundle b = new Bundle();
b.putString("username", username);
b.putString("message", message);
b.putString("mentions", mentions);
b.putString("day", day);
Toast.makeText(c, "New tweet saved, "+message, Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(c, TweezeeReceiver.class);
myIntent.putExtras(b);
PendingIntent pendingIntent;
if (updating) {
pendingIntent = PendingIntent.getBroadcast(c, md5(username+og), myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
} else {
pendingIntent = PendingIntent.getBroadcast(c, md5(username+message),
myIntent, 0);
}
AlarmManager alarmManager =
(AlarmManager)c.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
Integer.parseInt(wait)*60000, pendingIntent);
}