[英]ANDROID: inside Service class, executing a method for Toast (or Status Bar notification) from scheduled TimerTask
我试图从定期执行的计划TimerTask中执行Service中的{public void}方法。
此TimerTask定期检查条件。 如果为true,则通过{className}。{methodName}调用方法。
但是,按照Java的要求,如果我想将{className}与{.dot}一起使用,则该方法必须是{pubic static}方法。
问题是此方法用于使用Toast(Android弹出通知)和状态栏进行通知。要使用这些通知,必须使用
Context context = getApplicationContext();
但是,要使此方法起作用,该方法必须没有{static}修饰符,并且必须位于Service类中。
因此,基本上,我希望后台Service从计划的TimerTask中评估条件,并在Service类中执行一个方法。
有人可以帮助我使用Service的正确方法是什么,在循环评估时满足特定条件时调用一种方法?
这是实际的代码行:
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) :如果将修饰符更改为静态,则会发生{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);
}
}
找到了简单的解决方案。 毕竟这是一个琐碎的问题。
在方法之外声明有问题的数据成员(如{Content}和{Intent}并将其修改为静态可解决问题
不明白为什么我没有提出这个简单的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.