【问题标题】:How to implement the airplane mode in app如何在app中实现飞行模式
【发布时间】:2015-09-16 07:44:53
【问题描述】:

如您所知,Android 中有飞行模式功能设置。 如果您执行此功能,您将无法使用数据、互联网、呼叫、消息等。如何在应用中实现飞行模式功能?

  • 我不想通过我的应用程序使用互联网、数据、通话、搞乱。

喜欢飞行模式。

【问题讨论】:

    标签: android airplane


    【解决方案1】:

    请在清单文件中添加权限

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

    以及来自this BLOG的代码

    检查是否启用:

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

    切换它:

    // 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);
    

    要获得有关状态变化的通知:

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

    【讨论】:

    • 感谢您的帮助。
    • 对不起,我昨天确实注册了。所以我不知道如何接受你的回答。请告诉我怎么做。
    • 出现错误 - '权限只授予系统应用'
    猜你喜欢
    • 1970-01-01
    • 2016-08-29
    • 2020-02-16
    • 2010-11-27
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    相关资源
    最近更新 更多