ANDROID: inside Service class, executing a method for Toast (or Status Bar notification) from scheduled TimerTask
<p>I am trying to execute a {public void} method in Service, from scheduled TimerTask which is periodically executing.</p>

<p>This TimerTask periodically checks a condition. If it's true, it calls method via {className}.{methodName};</p>

<p>However, as Java requires, the method needs to be {pubic static} method, if I want to use {className} with {.dot}</p>

<p>The problem is this method is for notification using Toast(Android pop-up notification) and Status Bar
To use these notifications, one must use</p>

<pre><code>Context context = getApplicationContext();
</code></pre>

<p>But for this to work, the method must not have {static} modifier and resides in Service class.</p>

<p><strong>So, basically, I want background Service to evaluate condition from scheduled TimerTask, and execute a method in Service class.</strong></p>

<p>Can anyone help me what's <strong>the right way to use Service, invoking a method when certain condition is satisfied while looping evaluation?</strong></p>

<p>Here are the actually lines of codes: </p>

<p><em><strong>The TimerTask class (WatchClipboard.java)</strong> :</em></p>

<pre><code>public class WatchClipboard extends TimerTask {

    //DECLARATION
    private static GetDefinition getDefinition = new GetDefinition();

    @Override
    public void run() {
        if (WordUp.clipboard.hasText()) {
            WordUp.newCopied = WordUp.clipboard.getText().toString().trim().toLowerCase();
            if (!(WordUp.currentCopied.equals(WordUp.newCopied))) {
                WordUp.currentCopied = WordUp.newCopied;    Log.v(WordUp.TAG, WordUp.currentCopied);

                getDefinition.apiCall_Wordnik();

                FetchService.instantNotification();   //it requires this method to have {static} modifier, if I want call in this way.
            }
        }       
    }
}
</code></pre>

<p><em><strong>And the Service class (FetchService.java)</em></strong> : If I change the modifier to static, {Context} related problems occur</p>

<pre><code>public class FetchService extends Service {
    public static final String TAG = "WordUp";  //for Logcat filtering


    //DECLARATION
    private static Timer runningTimer;
    private static final boolean THIS_IS_DAEMON = true;

    private static WatchClipboard watchClipboard;
    private static final long DELAY = 0;
    private static final long PERIOD = 100;

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {    Log.v(WordUp.TAG, "FetchService.onCreate()");
        super.onCreate();

        //TESTING SERVICE RUNNING
        watchClipboard = new WatchClipboard();
        runningTimer = new Timer("runningTimer", THIS_IS_DAEMON);
        runningTimer.schedule(watchClipboard, DELAY, PERIOD);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        runningTimer.cancel();

        stopSelf(); Log.v(WordUp.TAG, "FetchService.onCreate().stopSelf()");
    }

    public void instantNotification() {   //If I change the modifier to static, {Context} related problems occur
        Context context = getApplicationContext();  // application Context

        //use Toast notification: Need to accept user interaction, and change the duration of show
        Toast toast = Toast.makeText(context, WordUp.newCopied+": "+WordUp.newDefinition, Toast.LENGTH_LONG);
        toast.show();

        //use Status notification: need to automatically expand to show lines of definitions
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        int icon = R.drawable.icon;        // icon from resources
        CharSequence tickerText = WordUp.newCopied; // ticker-text
        long when = System.currentTimeMillis(); // notification time
        CharSequence contentTitle = WordUp.newCopied;   //expanded message title
        CharSequence contentText = WordUp.newDefinition;    //expanded message text

        Intent notificationIntent = new Intent(this, WordUp.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        // the next two lines initialize the Notification, using the configurations above
        Notification notification = new Notification(icon, tickerText, when);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        mNotificationManager.notify(WordUp.WORDUP_STATUS, notification);
    }
}
</code></pre>
