如何在android中检测整个应用程序的飞行模式

时间:2016-01-13 05:44:40

标签: java android android-intent airplane

我正在开发一款能够在整个应用程序中检测飞机模式的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;
    }       
}

2 个答案:

答案 0 :(得分: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" >

答案 1 :(得分:0)

创建一个类似 AirplaneModeManager 的界面,其实施扩展广播接收器,以便在激活飞行模式时获得通知

您可以在实现中使用此代码来保持跟踪并在整个应用程序中使用其方法来获取状态!!