When I call Toast from inside a service -a new tab with the message "Source not found" and "Timer.class" in tab title shows up
I am using Eclipse and running the App in debug mode. I have a class - AAStartsHere, derived from TabActivity. This derived class launches a service (AAservice). AAService setups a Timer/TimerTask AACallback. From inside this callback I setup a Toast using Notification. When AAcallback calls the Toast, the parameters passed to Toast seem OK but nothing appears or shows up on the screen, instead a new tab (titled Timer.class) shows up...
Here is the code fragment
AAStartsHere extends TabActivity {
:
Intent serviceIntent = new Intent (this, AAservice,...);
:
startservice(serviceIntent);
:
}
TimerTask execAACallback = newTimerTask { run() {AAcallback(); } };
AAService extends Service{
onCreate() {
:
AANotifcation = new Notification(....);
:
AATimer.scheduleAtFixedRate(execAACallback, ...)
}
AACallback() {
:
String svcName = Context.NOTIFICATION_SERVICE;
NotificationManager notiMgr = (NotificationManager) getSystemService(svcName);
Context context = getApplic开发者_开发技巧ationContext();
String text = "text goes here";
String title = "Title goes here";
Intent AAIntent = new Intent(AAService.this, AAStartsHere.class);
PendingIntent AAPendingIntent = PendingIntent.getActivity(context, 0, AAIntent, 0);
AANotification.setLatestEventInfo(context, title, text, AAPendingIntent);
AANotification.when = java.lang.System.currentTimeMillis();
notiMgr.notify(AA_NOTIFICATION_ID, AANotification);
Toast.makeText(context, title, Toast.LENGTH_LONG).show();
:
}
}
The new tab that shows up (in Eclipse/debug Mode) has the following text Class File Editor source not found The JAR of this class file belongs to container 'Android 2.1" which does not allow modifications to source attachments //Compiled from timer.java (version 1.5:49.0, super bit) :
Please let me know your thoughts - what am I missing? Thank you for the help and effort. Abhi
Sometimes toast doesn't show up from the service if it remains too long that encapsulates the time interval of the toast to be shown. Try to show the message of the toast at the end of the process of your service like :
AAStartsHere extends TabActivity {
:
Intent serviceIntent = new Intent (this, AAservice,...);
:
startservice(serviceIntent);
:
}
TimerTask execAACallback = newTimerTask { run() {AAcallback(); } };
AAService extends Service{
onCreate() {
:
AANotifcation = new Notification(....);
:
AATimer.scheduleAtFixedRate(execAACallback, ...)
}
AACallback() {
:
String svcName = Context.NOTIFICATION_SERVICE;
NotificationManager notiMgr = (NotificationManager) getSystemService(svcName);
Context context = getApplicationContext();
String text = "text goes here";
Intent AAIntent = new Intent(AAService.this, AAStartsHere.class);
PendingIntent AAPendingIntent = PendingIntent.getActivity(context, 0, AAIntent, 0);
AANotification.setLatestEventInfo(context, title, text, AAPendingIntent);
AANotification.when = java.lang.System.currentTimeMillis();
notiMgr.notify(AA_NOTIFICATION_ID, AANotification);
:
}
onStartCommand() {
String title = "Title goes here";
AACallBack();
Toast.makeText(context, title, Toast.LENGTH_LONG).show();
}
}
getApplicationContext() returns you an instance of your Application class, not an activity. You can't use this to display toasts, you must use a context of an activity.
精彩评论