如何在android中启用/禁用飞行模式?

时间:2012-03-06 15:56:57

标签: android

我正在编写一个Android应用程序,需要在特定时间段内禁用手机的手机信号,并需要启用信号。有人可以帮我这个吗?

提前致谢。


嗨,感谢以下答案,这适用于虚拟设备。但这不适用于真实设备(三星Galaxy y):(,有人可以告诉,为什么会这样?

2 个答案:

答案 0 :(得分:1)

试试这个:

public void onClick(View v){
        context = getApplicationContext();
    if (((ToggleButton)v).isChecked()){
        boolean isEnabled = Settings.System.getInt(context.getContentResolver(),
                Settings.System.AIRPLANE_MODE_ON, 0) == 1;
        if(isEnabled == false)
        {
        Settings.System.putInt(context.getContentResolver(),
                Settings.System.AIRPLANE_MODE_ON,1);
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", 1);
        context.sendBroadcast(intent);
        }



        }else
        {

            Settings.System.putInt(context.getContentResolver(),
                    Settings.System.AIRPLANE_MODE_ON,0);
            Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
            intent.putExtra("state", 0);
            context.sendBroadcast(intent);

        }

};
});

在代码的else部分,将第一个更改为数字0。

答案 1 :(得分:0)

点击此处

http://dustinbreese.blogspot.com/2009/04/andoid-controlling-airplane-mode.html

//切换飞行模式。

  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);
sendBroadcast(intent);
where isEnabled is whether airplane mode is enabled or not.
相关问题