开发者

ANDROID: inside Service class, executing a method for Toast (or Status Bar notification) from scheduled TimerTask

I am trying to execute a {public void} method in Service, from scheduled TimerTask which is periodically executing.

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

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

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

Context context = getApplicationContext();

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

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

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

Here are the actually lines of codes:

The TimerTask class (WatchClipboard.java) :

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.
            }
        }       
    }
}

And the Service class (FetchService.java) : If I change the modifier to static, {Context} related problems occur

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.makeT开发者_如何学运维ext(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);
    }
}


Found simple solution. It was trivial issue after all.

Declaring troubled data members like {Content} and {Intent} outside of the method and modify it to be static solves problem

Can't understand why I didn't come up with this simple solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜