我想在非活動類的靜態方法中啓動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;
}
}
所以我嘗試使用此背景下,但沒有做的推出敬酒。
能夠顯示敬酒,你所需要的活動,而不是一個Context – injecteer 2014-08-27 15:11:32
我還是不明白你的問題,你可以從一個服務創建一個連吐司沒有活動。你有什麼問題?您的代碼在哪裏顯示Toast – 2014-08-27 15:34:02