1

アプリケーション全体で機内モードを検出する Android アプリの開発に取り組んでいます。このコードをどこに追加しますか?

@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static boolean isAirplaneModeOn(Context context) {        
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        return Settings.System.getInt(context.getContentResolver(), 
                Settings.System.AIRPLANE_MODE_ON, 0) != 0;          
    } else {
        return Settings.Global.getInt(context.getContentResolver(), 
                Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
    }       
}
4

2 に答える 2

1

接続が利用可能かどうかを検出したい場所にこのコードを追加できます。

例えば:

@Override
    public void onClick(View v) {
boolean isonair = myApp.isAirplaneModeOn(this);
Toast.makeText(this, "IS on AIR? " + isonair, Toast.LENGTH_LONG).show();
}

静的関数にアクセスしたい場合は、それらを Application クラスで使用します。

public class myApp extends Application {
public static function isAirplaneModeOn() {
...
}
}

どのアクティビティでも、次のアクセスを使用します。myApp.isAirplaneModeOn()

あなたの更新を忘れないでくださいAndroidManifest.xml

<application
        android:largeHeap="true"
        android:name=".myApp" <<<<<<<<<<<<<<<<<<< this is your class name
        android:icon="@drawable/somedrawable"
        android:label="@string/app_alias"
        android:theme="@style/AppTheme" >
于 2016-01-13T05:47:26.843 に答える
0

飛行機モードがアクティブ化されたときにシナリオで通知を受け取るためにブロードキャスト レシーバーを拡張する実装を持つAirplaneModeManagerのようなインターフェイスを作成します。

実装でこのコードを使用して追跡し、アプリケーション全体でそのメソッドを使用してステータスを取得できます!!

于 2016-01-13T05:57:22.443 に答える