2012-06-07 64 views
0

我用一个按钮创建了一个应用程序,允许开启和关闭飞行模式。打开飞行模式时可以正常工作。但奇怪的是,飞机模式在切断时似乎仍然存在。无法关闭飞行模式

如果我在关闭手机后检查手机设置,则表示飞机模式已关闭。但飞行模式图标仍然显示在手机的顶部栏中,并按住手机的电源按钮显示飞行模式仍然打开。

不确定为什么设置会在关闭时显示为关闭状态?

这里是我用来关闭它的代码 - 我已经调试过了,它绝对是这样的。 mContext是我用来保存上下文的变量,这是传递到设置类,然后在方法开启飞行模式和关闭:

System.putInt(mContext.getContentResolver(), 
android.provider.Settings.System.AIRPLANE_MODE_ON, 0); 
this.airplaneOn = false; 
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); 
intent.putExtra("state", 0); 
mContext.sendBroadcast(intent); 

而且这里是我使用的检查代码的飞行模式状态 -

public boolean isAirplaneOn() { 
    int airplaneMode = 0; 
    try { 
    airplaneMode = System.getInt(mContext.getContentResolver(), 
    android.provider.Settings.System.AIRPLANE_MODE_ON); 
    } catch (SettingNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    if (airplaneMode == 1) { 
     this.airplaneOn = true; 
    } else { 
     this.airplaneOn = false; 
    } 
    return airplaneOn; 
} 

在这两种情况下,this.airplaneOn是存储的飞行模式状态的私人布尔。

我可以在这里做一些愚蠢的事,或者检查这个设置有点不可靠吗?

回答

4

我已经成功做了下面的代码:

public static boolean isFlightModeEnabled(Context context) { 
    return Settings.System.getInt(context.getContentResolver(), 
     Settings.System.AIRPLANE_MODE_ON, 0) == 1; 
} 

public static void toggleFlightMode(Context context) { 
    boolean isEnabled = isFlightModeEnabled(context); 

    Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1); 

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

我我的代码和你之间看到的唯一区别是,在你的putInt()你有硬编码的0

一另外要注意的是根据文档,ACTION_AIRPLANE_MODE_CHANGED

This is a protected intent that can only be sent by the system.

我没有在这个问题了过去,但也许你的设备更新,并强制执行那些我已经取得成功的旧手机不执行。

+0

这是破解它 - 传递一个布尔代替0/1确实使它的工作。非常好,谢谢! – vinnyh

1

为您的意图使用布尔代替0/1,即:intent.putExtra(“state”,true)。其他帖子中的代码应该正常工作。

+0

是它的伟大工程 - 通过布尔的意图没有的伎俩! – vinnyh

0

请参考下面的代码:

@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: 

// //