Hi

final Context context = getApplicationContext();
        final Handler handler = new Handler(new Callback() {

            public boolean handleMessage(Message msg) {
                final Toast t = new Toast(context);
                Toast makeText = t.makeText(context, "Test Toast",
Toast.LENGTH_SHORT);
                makeText.show();

                return true;
            }
        });
        Timer timer = new Timer( true);

        timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                Log.d("Timertask", "Tick");
                handler.sendMessage(new Message());
            }
        }, new Date(),2000);


This works .. haven't tried it in a service though.. but work in an
activity (so i think it works also)



On Jan 7, 9:35 am, Manoj <[email protected]> wrote:
> ya you are right but in my code in doServiceWork() method its not
> working
>
> public class TestService extends Service
> {
>
>     public static FileScanner MAIN_ACTIVITY;
>
>     private Timer timer=new Timer();
>
>     private static long UPDATE_INTERVAL = 1*60*10;  //default
>
>     private static long DELAY_INTERVAL = 0;
>
>     // hooks main activity here
>     public static void setMainActivity(FileScanner activity)
>     {
>       MAIN_ACTIVITY = activity;
>     }
>
>     /*
>      * not using ipc...but if we use in future
>      */
>     public IBinder onBind(Intent intent) {
>       return null;
>     }
>
>     @Override
>     public void onCreate()
>     {
>       super.onCreate();
>
>       _startService();
>
>       if (MAIN_ACTIVITY != null)  Log.d(getClass().getSimpleName(),
> "FileScannerService started");
>     }
>
>     @Override
>     public void onDestroy()
>     {
>       super.onDestroy();
>
>       _shutdownService();
>
>       if (MAIN_ACTIVITY != null)  Log.d(getClass().getSimpleName(),
> "FileScannerService stopped");
>     }
>
>     /*
>      * starting the service
>      */
>     private void _startService()
>     {
>       timer.scheduleAtFixedRate(
>
>               new TimerTask() {
>
>                     public void run() {
>
>                         try{
>
>                         doServiceWork();
>
>                         Thread.sleep(UPDATE_INTERVAL);
>
>                         }catch(InterruptedException ie){
>
>                             Log.e(getClass().getSimpleName(),
> "FileScannerService InterruptedException"+ie.toString());
>                         }
>
>                     }
>                   },
>                   DELAY_INTERVAL,
>                   UPDATE_INTERVAL);
>
>       Log.i(getClass().getSimpleName(), "FileScannerService Timer
> started....");
>     }
>
>     /*
>      * start the processing, the actual work, getting config params,
> get data from network etc
>      */
>     private void doServiceWork()
>     {
>
> ///code for work to be done
>
>     }
>
>     /*
>      * shutting down the service
>      */
>     private void _shutdownService()
>     {
>       if (timer != null) timer.cancel();
>       Log.i(getClass().getSimpleName(), "Timer stopped...");
>     }
>
> }
>
> On Jan 7, 1:15 pm, Nymann <[email protected]> wrote:
>
>
>
> > Hi
>
> > Maybe something like the following ... (i havent tried the code)
>
> > Timer timer = new Timer( true);
>
> >         timer.scheduleAtFixedRate(new TimerTask() {
>
> >             @Override
> >             public void run() {
> >                 Toast t = new Toast(context);
> >                 t.makeText(context, "Test Toast",
> > Toast.LENGTH_SHORT);
> >                 t.show();
>
> >             }
> >         }, new Date(),3000);
>
> > Kind regards Per
>
> > On Jan 7, 7:28 am, Manoj <[email protected]> wrote:
>
> > > Hi,
>
> > > Please help me out on the issue as I want to make a Toast to promt
> > > again and again on screen by a service running in background. Please
> > > suggest me the way how to make this done.
>
> > > manoj chauhan
-- 
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