简体   繁体   English

如何在应用程序中实现飞行模式

[英]How to implement the airplane mode in app

As you know, there is Airplane Mode feature setting in Android. 如您所知,Android中有“飞行模式”功能设置。 if you execute this feature you can't use the data, internet, calling, messing and so on. 如果执行此功能,则无法使用数据,互联网,呼叫,混乱等。 How can I implement a Airplane Mode feature in app? 如何在应用中实现飞行模式功能?

  • I want to not be able to use the Internet, data, calling, messing from my App. 我希望无法使用我的App中的Internet,数据,呼叫,混乱。

Like Airplane Mode. 就像飞机模式。

Please add permission in manifest file 请在清单文件中添加权限

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

and in code from this BLOG 并在此BLOG的代码中

Check to see if it is enabled or not: 检查是否已启用:

boolean isEnabled = Settings.System.getInt(
      context.getContentResolver(), 
      Settings.System.AIRPLANE_MODE_ON, 0) == 1;

To toggle it: 要切换它:

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

To get notifications on state change: 要获取有关状态更改的通知:

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");
      }
}

context.registerReceiver(receiver, intentFilter);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM