【问题标题】:ANDROID: inside Service class, executing a method for Toast (or Status Bar notification) from scheduled TimerTaskANDROID:在 Service 类中,从计划的 TimerTask 执行 Toast(或状态栏通知)的方法
【发布时间】:2010-04-30 03:58:14
【问题描述】:

我正在尝试从定期执行的定时 TimerTask 中执行 Service 中的 {public void} 方法。

此 TimerTask 定期检查条件。如果为真,则通过 {className}.{methodName}; 调用方法;​​

但是,根据 Java 的要求,如果我想将 {className} 与 {.dot} 一起使用,该方法必须是 {public static} 方法

问题是这个方法是使用 Toast(Android 弹出通知)和状态栏的通知 要使用这些通知,必须使用

Context context = getApplicationContext();

但要使其工作,该方法不得具有 {static} 修饰符并且驻留在 Service 类中。

所以,基本上,我希望后台服务从计划的 TimerTask 中评估条件,并在服务类中执行一个方法。

谁能帮助我使用服务的正确方法是什么,在循环评估时满足特定条件时调用方法?

以下是实际的代码行:

TimerTask 类 (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.
            }
        }       
    }
}

还有Service类(FetchService.java):如果我把修饰符改成static,就会出现{Context}相关的问题

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);
    }
}

【问题讨论】:

    标签: java android


    【解决方案1】:

    找到了简单的解决方案。毕竟是小事。

    在方法之外声明有问题的数据成员,例如 {Content} 和 {Intent} 并将其修改为静态即可解决问题

    不明白为什么我没有想出这个简单的解决方案。

    【讨论】:

      猜你喜欢
      • 2011-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多