0

非アクティビティ クラスの静的メソッドでトースト メッセージを起動したいと考えています。私はそれについて多くのスレッドを読みましたが、私の状況はもう少し複雑です。特に:

サービスがあり、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;
}
}

したがって、このコンテキストを使用してトーストを起動しようとしましたが、何もしません。

4

3 に答える 3

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 を表示するために私が何をしたかを理解するために、ハンドラーの詳細を読むことができます。

于 2014-08-27T15:21:46.353 に答える
0

あなたのサービスのスーパークラスは何ですか? ToastメインUIスレッドに投稿していないため、表示されていないよりもIntentServiceの場合。これを次のように行う必要があります。

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);
});
于 2014-08-27T15:26:25.603 に答える
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());
于 2014-08-27T15:09:57.140 に答える