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";


        @Override
        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
-~----------~----~----~----~------~----~------~--~---

Reply via email to