Here's how to do it:
public class AlarmFired extends BroadcastReceiver {
private String type;
private int position;
private String reason;
private String value;
private String rbTax;
private static final int uniqueId = (int)System.currentTimeMillis();
@Override
public void onReceive(Context context, Intent intent)
{
Log.d("exp", "**************BUENO JD1 **********************");
Bundle extras = intent.getExtras();
if (extras != null) {
Log.d("exp", "**************BUENO JD1+
**********************");
position = extras.getInt("position");
type = extras.getString("type");
value = extras.getString("value");
reason = extras.getString("reason");
showNotification(context);
//setNextAlarm(context);
Log.d("exp", "**************BUENO JD4
**********************");
} else {
Log.d("exp", "**************BUENO2
**********************");
}
}
private void showNotification(Context context) {
Log.d("exp", "**************BUENO JD100
**********************");
NotificationManager mgr =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Log.d("exp", "**************BUENO JD5a
**********************");
int icon = (type.equalsIgnoreCase("expense")) ?
R.drawable.ico_statusbar_expense : R.drawable.ico_statusbar_income;
Log.d("exp", "**************BUENO JD5b
**********************");
Notification note = new Notification(icon, "Wallet Manager",
System.currentTimeMillis());
Log.d("exp", "**************BUENO JD6 **********************");
Intent intent = new Intent(context, RescheduleAlarm.class);
intent.putExtra("notificationId", uniqueId);
intent.putExtra("position", position); // Spinner
position defining
repetition
intent.putExtra("reason", reason);
intent.putExtra("value", value);
intent.putExtra("rbTax", rbTax);
intent.putExtra("type", type);
PendingIntent i = PendingIntent.getActivity(context, 0, intent,
0);
Log.d("exp", "**************BUENO JD5d**********************");
note.setLatestEventInfo(context, "Notification Title", "This is
the
notification message", i);
Log.d("exp", "**************BUENO JD5e**********************");
note.vibrate = new long[] { 100, 250, 100, 500};
Log.d("exp", "**************BUENO JD6e **********************");
Log.d("exp", "**************BUENO JD7a **********************");
mgr.notify(uniqueId, note);
Log.d("exp", "**************BUENO JD7 **********************");
}
public void setNextAlarm(Context context) {
Intent intent = new Intent(context, AlarmFired.class);
intent.putExtra("position", position); // Spinner
position defining
repetition
intent.putExtra("reason", reason);
intent.putExtra("value", value);
intent.putExtra("rbTax", rbTax);
intent.putExtra("type", type);
PendingIntent sender = PendingIntent.getBroadcast(context, 0,
intent, 0);
AlarmManager am =
(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
long nextAlarm = getNextAlarmMillis(position);
//am.set(AlarmManager.RTC_WAKEUP, nextAlarm, sender);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 20);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
sender);
}
On Nov 1, 1:23 pm, "Juan David Trujillo C." <[EMAIL PROTECTED]>
wrote:
> Hi Megha!
>
> I'm having a similar problem. I want to be able to create a
> notification and schedule an alarm from a BroadcastReceiver class. Do
> I have to make my BroadcastReceiver an inner class of an
> Activity or is it just using a regular class that extends
> BroadcastReceiver? Is it possible to launch other intents from a
> BroadcastReceiver to trigger logic in an activity?
>
> On Oct 21, 2:48 am, "Megha Joshi" <[EMAIL PROTECTED]> wrote:
>
> > One of the options is to make your BroadcastReceiver an inner class of your
> > Activity. It also makes sense to make BroadCasterReceiver as Activity's
> > inner class if its just going to update your Activity's UI.
>
> > 2008/10/20 Abraham <[EMAIL PROTECTED]>
>
> > > HI ,
>
> > > I'm trying the understand how an application can receive intent in
> > > BroadcastReceiver & how it can inform the Activity abt the information
> > > received in the intent.
>
> > > For this purpose I have created an activity which has a TextView. In
> > > OnCreate of the Activity I'm using AlarmManager to start sending
> > > intents every 10 sec to broadcast receiver of my application. On
> > > receiving this intent I would like to update the text view with the
> > > current time.
>
> > > From the documention I understand that we need to be using the
> > > NotificationManager instead of trying to directly updating the UI.
>
> > > Can some one pls let me know how I can achieve this.
>
> > > Code snippets:
> > > public class myFaves extends Activity
> > > {
> > > public void onCreate(Bundle savedInstanceState)
> > > {
> > > super.onCreate(savedInstanceState);
> > > setContentView(R.layout.main);
> > > mCurrentTime = (TextView) findViewById(R.id.curtime_label);
> > > startUpdatingCurrentTime();
> > > }
>
> > > protected void onPause()
> > > {
> > > super.onPause();
> > > cancelUpdatingCurrentTime();
> > > }
>
> > > private void startUpdatingCurrentTime()
> > > {
> > > Intent intent = new Intent(MyTimer.this, MyTimerReceiver.class);
> > > PendingIntent sender = PendingIntent.getBroadcast(MyTimer.this, 0,
> > > intent, 0);
>
> > > // We want the alarm to go off every second from now.
> > > long firstTime = SystemClock.elapsedRealtime();
> > > firstTime += 10000;
>
> > > // Schedule the alarm!
> > > AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
> > > am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,
> > > 10000, sender);
>
> > > Calendar calendar = Calendar.getInstance();
> > > calendar.setTimeInMillis(System.currentTimeMillis());
> > > mCurrentTime.setText( calendar.getTime().toString());
> > > }
>
> > > public void cancelUpdatingCurrentTime()
> > > {
> > > Intent intent = new Intent(MyTimer.this, MyTimerReceiver.class);
> > > PendingIntent sender = PendingIntent.getBroadcast(MyTimer.this, 0,
> > > intent, 0);
>
> > > // And cancel the alarm.
> > > AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
> > > am.cancel(sender);
> > > }
> > > }
>
> > > public class MyTimerReceiver extends BroadcastReceiver
> > > {
> > > protected static final String LOG_TAG = "MyTimer";
>
> > > [EMAIL PROTECTED]
> > > public void onReceive(Context context, Intent intent)
> > > {
> > > Log.d(LOG_TAG, "onReceive");
> > > // How do I update the TextView with
> > > current time in my activity?
> > > }
>
> > > }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---