I have an app with an activity and a service.  The service schedules a
function to run using timer.scheduleAtFixedRate. When the timer task
runs, I want to update some TextView objects in the Activity's UI, so
that the user knows that the service has run, and the last thing it
did.

So in the timer task I do:
                Intent intent = new
Intent("com.edgreenberg.TextMessageServer.UPDATE_SCREEN");
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd
HH:mm:ss");
                Date date = new Date();
                intent.putExtra("time", dateFormat.format(date));
                intent.putExtra("numberMsgs",numberMsgs);
                sendBroadcast(intent);

Then, in the activity, I created:

        private int numMsgs1;
        private String dateTime1;

        public class CustomBroadCast extends BroadcastReceiver {
                public CustomBroadCast(){
                }
            /**
             * @see
android.content.BroadcastReceiver#onReceive(Context,Intent)
             */
                        //public static final String TAG = "CustomBroadCast";

                        @Override
                        public void onReceive(Context context, Intent intent) {
        
if(intent.getAction().equals("com.edgreenberg.TextMessageServer.UPDATE_SCREEN"))
{
                                Log.i("INFORMATION","Broadcasting message");
                                dateTime1 = intent.getStringExtra("time");
                                numMsgs1 = intent.getIntExtra("numberMsgs", 0);
                                // gotta get this to the gui thread somehow.
                                handler.sendEmptyMessage(0);


                                }
            }

        }

Now, where it says "gotta get this to the gui thread somehow" is where
I previously did dateTime.setText(dateTime1), but according to what I
read, that shouldn't work. And it doesn't :)

So I sent the empty message using the handler, as shown below:

        private Handler handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                        final TextView dateTime,numMsgs;
                        dateTime = (TextView) findViewById(R.id.datetime);
                        numMsgs = (TextView) findViewById(R.id.numMsgs);
                        dateTime.setText(dateTime1);
                        numMsgs.setText(numMsgs1);
        }

        };
 Now this, I expect to work. It doesn't. The screen stays mute.  In
fact, the debugger tells me that:
Thread [<3> main] (Suspended (exception Resources$NotFoundException))
        Resources.getText(int) line: 205
        TextView.setText(int) line: 2809
        TextMessageControlPanel$1.handleMessage(Message) line: 38
        TextMessageControlPanel$1(Handler).dispatchMessage(Message) line: 99
        Looper.loop() line: 123
        ActivityThread.main(String[]) line: 4203
        Method.invokeNative(Object, Object[], Class, Class[], Class, int,
boolean) line: not available [native method]
        Method.invoke(Object, Object...) line: 521
        ZygoteInit$MethodAndArgsCaller.run() line: 791
        ZygoteInit.main(String[]) line: 549
        NativeStart.main(String[]) line: not available [native method]

It feels like something is out of scope, but I'm not java-literate
enough to tell what.

My handler,  CustomBroadCast class, onCreate, onStart are all at the
same level inside my main activity class (extends activity.)

Can somebody shed some light?

Thanks,
</edg>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to