当我从服务内部调用 Toast 时,会出现一个带有消息“未找到源”的新选项卡和“Timer.class”在选项卡标题中显示

发布于 2024-09-19 02:24:54 字数 1586 浏览 3 评论 0原文

我正在使用 Eclipse 并在调试模式下运行该应用程序。 我有一个类 - AAStartsHere,派生自 TabActivity。该派生类启动一个服务 (AAservice)。 AAService 设置一个 Timer/TimerTask AACallback。在这个回调中,我使用通知设置了一个 Toast。当 AAcallback 调用 Toast 时,传递给 Toast 的参数似乎正常,但屏幕上没有出现或显示任何内容,而是显示一个新选项卡(标题为 Timer.class)...

这是代码片段

 
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"; 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(); : } }

显示的新选项卡(在 Eclipse/调试模式下)包含以下文本 类文件编辑器 未找到来源 该类文件的 JAR 属于容器“Android 2.1”,不允许修改源附件 //由timer.java编译而来(版本1.5:49.0,超级位) :

请让我知道你的想法 - 我错过了什么?感谢您的帮助和努力。 阿比

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 = getApplicationContext(); 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

累赘 2024-09-26 02:24:54

有时,如果 Toast 保持太长,封装了要显示的 Toast 的时间间隔,则服务不会显示 Toast。尝试在服务过程结束时显示 toast 消息,例如:

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

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();
  }
}
撩心不撩汉 2024-09-26 02:24:54

getApplicationContext() 返回您的 Application 类的实例,而不是活动。您不能使用它来显示 toast,您必须使用活动的上下文。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文