2014-08-27 49 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

相關問題