2014-08-27 80 views
0

我想在非活动类的静态方法中启动Toast消息。 我读了很多关于这个的线程,但我的情况有点复杂,特别是:在非活动类的静态方法中启动Toast

我有一个服务,并在OnStartCommand我用固定的时间间隔调用另一个类的静态方法,在这个被称为方法我想在某些特定情况下显示吐司消息。

我也尝试创建一个支持类,它扩展了应用程序,在该应用程序中我有应用程序上下文以在需要时获取,但无所事事。

这是调用静态方法AllInterfacesActived服务类中的方法:

public int onStartCommand(Intent intent, int flags, int startId) { 

    //flag variable that indicates if service is scanning 
    isScanning = true; 

    final int result; 

    turnGPSOn(); 


    /*GET WIFI DATA, we use a thread and with scheduleAtFixedRate we run it repeatedly with the wifi scan interval*/ 
    Wifitimer.scheduleAtFixedRate(new TimerTask() { 


     @Override 
     public void run() { 
      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime()); 

      //we are in wifi case, we set umts string result to "" because we don't perform a umts scansion 
      String resultUMTS = ""; 

      //call the SCANWIFI METHOD to scan the networks and to obtain their data 
      String resultScan = scanWifi(); 
      Intent mIntent = new Intent(); 
      mIntent.setAction(INTENT_ACTION); 

      //put information on wifi and umts in the intent 
      mIntent.putExtra(INTENT_EXTRA_WIFI, "Waiting for umts scansion...\nWIFI SCAN PERFOMED"+resultScan+"\nUMTS\n"+resultUMTS); 

      //broadcast of data 
      Bundle xtra = new Bundle(); 
      sendBroadcast(mIntent); 

      /* 
      * when all interfaces are actived we call AllInterfacesActived() of OracoloBrain.java 
      */ 
      if(getUseOracolo()) 
       if(isAPNEnabled(getApplicationContext()) && isGpsEnable() && isWifiEnabled()){ 
        OracoloBrain.AllInterfacesActived();  
       } 


     } 
    }, 0,MainActivity.getWifiInterval()); 

//other code of the onStartCommand method... 

在OracoloBrain类(非活性类)我有静态方法AllInterfacesActived。 我忽略了关于此方法的代码,但在特定情况下,我想显示Toast。 我尝试创建另一个类叫做MyApplication.java:

public class MyApplication extends Application { 

private static Context context; 

public void onCreate(){ 
    super.onCreate(); 
    MyApplication.context = getApplicationContext(); 
} 

public static Context getAppContext() { 
    return MyApplication.context; 
} 
} 

所以我尝试使用此背景下,但没有做的推出敬酒。

+0

能够显示敬酒,你所需要的活动,而不是一个Context – injecteer 2014-08-27 15:11:32

+0

我还是不明白你的问题,你可以从一个服务创建一个连吐司没有活动。你有什么问题?您的代码在哪里显示Toast – 2014-08-27 15:34:02

回答

1

您可以在主线程上创建一个处理程序对象,然后稍后可以使用它来显示您的敬酒。下面是一个示例代码,可以帮助你。

class MyService extends Service{ 
Handler handler ; 
onStartCommand(intent int , int flags,int startId){ 
handler = new Handler(); // this will get instantiated on the main thread; 
new Thread(new Runnable() { 

        @Override 
        public void run() { 
         dispatchMessage("this is a toast"); 

        } 
       }).start(); 


} 
public void dispatchMessage(final String message) { 
     handler.post(new Runnable() { 
      @Override 
      public void run() { 
       System.out.println(message); 
       Toast.makeText(MyService.this, message, Toast.LENGTH_SHORT).show(); 
      } 
     }); 

    } 

} 

你可以阅读更多关于处理程序来了解我已经做了什么来显示除了主线程以外的其他线程上的Toast。

+0

这是正确的方法。我使用该代码来找到解决方案。谢谢。 – 2014-08-27 15:58:43

0

您可以将应用程序的上下文类:

在您的非活性类:后

private static Context c; 

public static Context getAndSetMyContext(Context c) { 
this.c = c; 
} 

,您可以:

Toast.makeText (c, "YOUR TEXT", Toast.LENGTH_LONG).show(); 

而在你的活动类:

YourClass.getAndSetMyContext(getBaseContext()); 
+0

警告:始终将应用程序上下文保存在静态字段 – 2014-08-27 15:23:27

0

什么是您的服务的超级类?如果它的IntentService比Toast没有被显示,因为你没有在主UI线程上发布它。你必须这样做以这样的方式

Handler handler = new Handler(Looper.getMainLooper()); 
handler.post(new Runnable { 
    @Override 
    public void run() { 
     Toast.makeText(context/*Use app context here from your Application subclass*/, "", Toast.SHORT); 
}); 
+0

超类是Service – 2014-08-27 15:28:29

相关问题