在 Android 中切换飞行模式
我做错了吗?它不起作用。
public void airplane() {
boolean isEnabled = Settings.System.getInt(this.getApplicationContext().getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1;
Settings.System.putInt(context.getContentResolver(),Settings.System.AIRPLANE_MODE_ON,isEnabled ? 0 : 1);
//Settings.System.putInt(this.getApplicationContext().getContentResolver(),Settings.System.AIRPLANE_MODE_ON,isEnabled ? 0 : 1);
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
}
Did I make a mistake? It's not working.
public void airplane() {
boolean isEnabled = Settings.System.getInt(this.getApplicationContext().getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1;
Settings.System.putInt(context.getContentResolver(),Settings.System.AIRPLANE_MODE_ON,isEnabled ? 0 : 1);
//Settings.System.putInt(this.getApplicationContext().getContentResolver(),Settings.System.AIRPLANE_MODE_ON,isEnabled ? 0 : 1);
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
此答案包含必要的代码做这个。另请确保您拥有
WRITE_SETTINGS
权限。改编自控制飞行模式:
This answer contains code necessary to do this. Also make sure you have the
WRITE_SETTINGS
permission.Adapted from Controlling Airplane Mode:
适用于所有 API 版本。
API >= 17 的用户操作模式
无 root,无系统级权限!
在 AndroidManifest.xml 添加权限:
然后:
Works for all API versions.
mode for user actions for API >= 17
no root, no system level permission!
in AndroidManifest.xml add permission:
then:
以下内容可在已取得 root 权限的设备上使用。
从命令行,您可以使用以下命令打开/关闭飞行模式:
这适用于 android 4.4.2+
The following can be used on a rooted device.
From commandline, you can toggle airplane mode on/off with the following:
This works with android 4.4.2+
注意:NarendraR指出这不适用于最新版本。
我认为“Settings.System.AIRPLANE_MODE_ON”已被弃用,我正在使用:
我希望这对某人有帮助。
Note: it was pointed out by NarendraR that this is not working on the latest version.
I think "Settings.System.AIRPLANE_MODE_ON" is deprecated, Im using:
I hope this help someone.
正如 @eggyal 提到的,从 4.2 及更高版本开始,无法切换飞行模式。
但我们可以做的是打开每个无线服务:
可以使用 WifiService ~ getSystemService(Context.WIFI_SERVICE) 来控制 Wifi。
可以使用BluetoothAdapter-getSystemService(Context.BLUETOOTH_SERVICE) 来控制蓝牙。
经过一些研究,我发现使用 Java Reflection 你仍然可以控制 Android 无线电(Wifi、网络、蓝牙),飞行模式基本上所做的就是切换蓝牙、Wifi 和网络的状态。
因此,通过控制无线电,您实际上可以创建自己的飞行模式。
警告:在某些设备上使用反射可能会失败(取决于类的制造商实现)。
此示例代码切换移动网络:
As @eggyal mentioned toggling Airplane Mode cannot be done from version 4.2 and up.
But what we can do is turn each of the wireless services:
Wifi can be controlled using WifiService ~ getSystemService(Context.WIFI_SERVICE).
Bluetooth can be controlled using BluetoothAdapter- getSystemService(Context.BLUETOOTH_SERVICE).
After some research I found that using Java Reflection you can still control the Android Radios (Wifi, Network, Bluetooth) what Airplane Mode basically does is toggle the state of Bluetooth, Wifi and Networks.
So by controlling the Radios you can in effect create your own Airplane Mode.
WARNING: Using Reflection might fail on some devices (depends on manufacturer implementation of the classes).
This sample code toggles Mobile Network:
在每个设置之前,请像这样使用 android.provider :
here before every settings use android.provider like this :
棒棒糖之后,您可以使用以下方法,但它是隐藏的api,您的应用程序需要系统级权限
After lollipop, you can use below method, but it is hidden api and your application needs system level permission
请注意,如 Android 4.2 API 下所述(已添加重点):
Note that, as documented under Android 4.2 APIs (emphasis added):
这个对我有用
,但你需要系统权限
This one works for me
But you need system permissions though
我发现这个解决方法适用于 17+ 如果你想关闭互联网,这会起作用。
许可
方法
I found this workaround for 17+ if you want to shut off the internet this will work.
permission
method