2013-07-30 75 views
0

我試圖上傳關閉GPS並啓用「平面模式」的應用程序,並且因爲錯誤導致Galaxy Tab Wi-Fi(M180W)被拒絕。所以我需要一些幫助來確定設備是否具有平面模式功能或GPS。如何檢查設備是否具有GSM或GPS功能?

GPS:

//gps 
     final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     appPrefs.setGPS(manager.isProviderEnabled(LocationManager.GPS_PROVIDER)); 
     if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER) && manager!=null){ 
     Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
     startActivity(myIntent); 
     Toast toast = Toast.makeText(getApplicationContext(), "Please turn off GPS and hit back", Toast.LENGTH_LONG); 
     toast.show(); 

平面模式:

// plane mode 
     if (option){ 
      appPrefs.setCBoption(true); 
      // read the airplane mode setting 
      boolean isEnabled = Settings.System.getInt(
         getApplicationContext().getContentResolver(), 
         Settings.System.AIRPLANE_MODE_ON, 0) == 1; 

      // toggle airplane mode 
      Settings.System.putInt(
        getApplicationContext().getContentResolver(), 
         Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1); 

      // Post an intent to reload 
      Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); 
      intent.putExtra("state", !isEnabled); 
      sendBroadcast(intent); 
+0

不能手動啓用GPS。用戶應該這樣做。即使你找到了方法,它可能不適用於所有設備。 – ARMAGEDDON

+0

是的,我知道我彈出一個窗口,用戶可以在其中選擇:) – whiteLT

+0

我需要一種方法來確定設備是否具有此功能,或者此功能在該設備中爲「null」 – whiteLT

回答

3

對於GSM:

TelephonyManager manager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE); 

if (manager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) 

對於GPS:

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

ArrayList<String> names = (ArrayList<String>) locationManager.getProviders(false); 

如果名稱爲空,則您沒有GPS

0

聽起來像要使用PackageManager.hasSystemFeature(...)

/** Returns true if this device has support for GSM, otherwise false. */ 
public static boolean hasGsmSupport(Context context) { 
    PackageManager pm = context.getPackageManager(); 
    return pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_GSM); 
} 

和...

/** Returns true if this device has support for GPS, otherwise false. */ 
public static boolean hasGpsSupport(Context context) { 
    PackageManager pm = context.getPackageManager(); 
    return pm.hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS); 
} 
相關問題