Java ANDROID:在服务类内部,执行来自预定TimerTask的Toast(或状态栏通知)方法
我正试图从定期执行的定时任务中执行服务中的{public void}方法 此时间任务定期检查一个条件。如果为true,则通过{className}.{methodName}调用方法 但是,正如Java所要求的,如果我想将{className}与{.dot}一起使用,那么该方法需要是{pubic static}方法 问题是此方法用于使用ToastAndroid弹出通知和状态栏进行通知 要使用这些通知,必须使用Java ANDROID:在服务类内部,执行来自预定TimerTask的Toast(或状态栏通知)方法,java,android,Java,Android,我正试图从定期执行的定时任务中执行服务中的{public void}方法 此时间任务定期检查一个条件。如果为true,则通过{className}.{methodName}调用方法 但是,正如Java所要求的,如果我想将{className}与{.dot}一起使用,那么该方法需要是{pubic static}方法 问题是此方法用于使用ToastAndroid弹出通知和状态栏进行通知 要使用这些通知,必须使用 Context context = getApplicationContext();
Context context = getApplicationContext();
但要使其工作,该方法必须不具有{static}修饰符,并且驻留在服务类中
所以,基本上,我希望后台服务从计划的TimerTask评估条件,并在服务类中执行一个方法
有谁能帮助我使用服务的正确方式是什么,在循环评估时,在满足特定条件时调用方法
以下是实际的代码行:
TimerTask类WatchClipboard.java:
服务类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);
}
}
找到了简单的解决方案。这毕竟是一个微不足道的问题 在方法外部声明有问题的数据成员,如{Content}和{Intent} 并将其修改为静态解决了问题 我不明白为什么我没有想出这个简单的解决办法
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);
}
}