1

我正在使用 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/调试模式下)具有以下文本 Class File Editor source not found 这个类文件的 JAR 属于容器“Android 2.1”,它不允许修改源附件 //Compiled from timer。 java(版本 1.5:49.0,超级位):

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

4

2 回答 2

0

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();
  }
}
于 2013-04-20T20:57:00.267 回答
0

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

于 2010-09-13T15:56:36.473 回答