我想在非活动类的静态方法中启动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