2016-01-13 56 views
1

我正在開發一個Android應用程序,該應用程序將在整個應用程序中檢測飛行模式。我在哪裏添加此代碼?如何在整個應用程序中檢測飛行模式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; 
    }  
} 
+0

將此方法添加到您的Java類:)。你的問題是什麼? –

+0

這取決於何時想檢測..在您的應用程序的開始? –

+0

您可以創建自己的自定義活動,並將此代碼放在那裏,並擴展您的自定義活動 –

回答

1

您可以將此代碼添加到要檢測連接是否可用的位置。

例如:

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

如果你想爲靜態功能的接入使用它們的應用程序類:在任何活動中使用

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" > 
+0

我想檢測整個應用程序在所有地方.. –

+0

@Rajkumar更新 – Vyacheslav

+0

真的!我們需要將靜態方法添加到Application類中? –

0

創建像AirplaneModeManager延伸廣播接收機其實施獲得的情況通報時,飛行模式被激活

您可以使用此代碼的執行,以保持軌道,並利用其在整個方法接口申請獲得狀態!