非アクティビティ クラスの静的メソッドでトースト メッセージを起動したいと考えています。私はそれについて多くのスレッドを読みましたが、私の状況はもう少し複雑です。特に:
サービスがあり、OnStartCommand で別のクラスの静的メソッドを一定間隔で呼び出します。この呼び出されたメソッドでは、特定のケースでトースト メッセージを表示したいと考えています。
また、アプリケーションを拡張するサポート クラスを作成しようとしました。このクラスには、必要なときに取得するアプリケーション コンテキストがありますが、何もする必要はありません。
これは、静的メソッド AllInterfacesActived を呼び出す Service クラスのメソッドです。
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;
}
}
したがって、このコンテキストを使用してトーストを起動しようとしましたが、何もしません。