2011-08-15 122 views
3

如果num> 50,我實現了這個代碼(從Toggle airplane mode in Android),但是在執行時我得到一個力量關閉,任何人都可以幫忙嗎?在Android中關閉飛行模式

   if(num>50){ 
        // read the airplane mode setting 
        boolean isEnabled = Settings.System.getInt(
          getContentResolver(), 
          Settings.System.AIRPLANE_MODE_ON, 0) == 1; 

        // toggle airplane mode 
        Settings.System.putInt(
          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); 



       } 

o.k.我實現了不祥的預感,但我想改變if語句:

if num>=50 and airplane mode=on toggle it off 
if airplane mode=off and num<50 toggle it on 

能有人幫我編寫新的代碼? (我是新手)

+5

冷靜並接受解答您的疑問,幫助你! – evilone

回答

8

你很可能沒有WRITE_SETTING權限添加到您的AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_SETTINGS" /> 

還要注意的是代碼:

Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); 
intent.putExtra("state", !isEnabled); 
sendBroadcast(intent); 

不應該工作,因爲根據文件ACTION_AIRPLANE_MODE_CHANGED

這是受保護的意圖,只能由系統發送。

即使您目前可以在沒有系統權限的情況下發送此廣播,它在未來的Android版本中可能會發生變化。

1

請參閱下面的代碼:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    IntentFilter intentFilter = new IntentFilter("android.intent.action.SERVICE_STATE"); 

    BroadcastReceiver receiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       Log.d("AirplaneMode", "Service state changed"); 
       Toast.makeText(getApplicationContext(), "Service state changed", Toast.LENGTH_LONG).show(); 
       boolean isEnabled = isAirplaneModeOn(context); 
      /* setSettings(context, isEnabled?1:0); 
       Intent intent_mode = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); 
       intent_mode.putExtra("state", !isEnabled); 
       context.sendBroadcast(intent_mode);*/ 

       if(isEnabled==true) 
       { setSettings(context, isEnabled?1:0); 
        Toast.makeText(getApplicationContext(), "Flight mode on", Toast.LENGTH_LONG).show(); 
        Settings.System.putInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0); 
        Intent newIntent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); 
        newIntent.putExtra("state", false); 
        sendBroadcast(newIntent); 
       } 
       else 
       { setSettings(context, isEnabled?1:0); 
        Toast.makeText(getApplicationContext(), "Flight mode off", Toast.LENGTH_LONG).show(); 
       } 

      } 

     @SuppressLint("NewApi") 
     private void setSettings(Context context, int value) { 
      // TODO Auto-generated method stub 

      if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { 
       Settings.System.putInt(
          context.getContentResolver(), 
          Settings.System.AIRPLANE_MODE_ON, value); 
      } else { 
       Settings.Global.putInt(
          context.getContentResolver(), 
          Settings.Global.AIRPLANE_MODE_ON, value); 
      }  

     } 

     @SuppressLint("NewApi") 
     public 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; 
     }  
    } 
    }; 

    registerReceiver(receiver, intentFilter); 


} 

//permissions needed: 

// //

+0

描述你做了哪些改變? – Avijit

+0

首先,使用brocast接收器,以便它在後臺工作...然後查看setSettings()方法,它用於版本相關的問題...和IntentFilter intentFilter = new IntentFilter(「android.intent.action.SERVICE_STATE」 );是重要的部分...讓我知道你的問題是否被回答... –