在 Android 上使用飞行模式时遇到问题

发布于 2024-12-26 11:23:40 字数 984 浏览 5 评论 0原文

我无法在我的应用程序上使用飞行模式。该应用程序在我的模拟器上运行得很好,但当我尝试我的 apk 时,它在我的真实手机上运行得不好。

这是激活飞机的代码:

Settings.System.putInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 1);
        // Post an intent to reload
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        // send a broadcast
        //intent.putExtra("state", !isEnabled);
        intent.putExtra("state", true);
        sendBroadcast(intent);

要停用它:

Settings.System.putInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);
        // Post an intent to reload
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        // send a broadcast
        intent.putExtra("state", false);
        sendBroadcast(intent);

我读到我无法创建意图(android doc),但我看到有人可以制作飞行模式,所以我不明白!

谢谢你帮助我!

I have troube to use Airplane mode on my app. The app runs perfectly well on my emulator but not in my real phone when I try my apk.

Here is the code to activate the Airplane :

Settings.System.putInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 1);
        // Post an intent to reload
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        // send a broadcast
        //intent.putExtra("state", !isEnabled);
        intent.putExtra("state", true);
        sendBroadcast(intent);

To desactivate it :

Settings.System.putInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);
        // Post an intent to reload
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        // send a broadcast
        intent.putExtra("state", false);
        sendBroadcast(intent);

I read that I cannot create the intent (android doc) but I saw people that could make airplane mode so I don't understand!

Thanks to help me !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

愿与i 2025-01-02 11:23:41
public static void setWifiOn(Context context) {
      WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    manager.setWifiEnabled(true);
    }

  public static boolean getAirplaneMode(Context context) {
      try {
      int airplaneModeSetting = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON);
      return airplaneModeSetting==1?true:false;
    } catch (SettingNotFoundException e) {
      return false;
    }
  }
public static void setWifiOn(Context context) {
      WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    manager.setWifiEnabled(true);
    }

  public static boolean getAirplaneMode(Context context) {
      try {
      int airplaneModeSetting = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON);
      return airplaneModeSetting==1?true:false;
    } catch (SettingNotFoundException e) {
      return false;
    }
  }
潇烟暮雨 2025-01-02 11:23:40

不能广播系统级意图。按照 Android 的标准,这是非法的。

与其尝试发送意图,为什么不实现一个广播接收器来监听该特定操作,然后当它发生变化时,让您的应用程序处理它。这将是一个更好的方法。您不能也不应该期望控制飞行模式,但您的应用程序可以在调用时进行侦听。您还可以从系统数据库检索一个属性,告诉您是否处于飞行模式。

再说一遍 不要专注于试图控制它,而只是处理它。

You CANNOT broadcast a System level intent. This is illegal by android's standards.

Instead of trying to send the intent, why don't you just implement a broadcast reciever to listen to that specific action and then when it changes, have your application handle it. This would be a much better approach. You cannot and should NOT expect to control airplane mode, but your app can listen to when it is invoked. You can also retrieve a property from the System database that tells you if its in airplane mode.

Once again Do not focus on trying to control it, just handle it.

旧竹 2025-01-02 11:23:40

作为一个合法的 Android 应用程序,您无法执行任何您想要执行的操作,包括将手机切换到飞行模式、禁用无线电、关闭声音或任何此类操作。

想象一下,如果某个任意应用程序关闭了您的手机收音机,并且您错过了通知您赢得一百万美元的电话,您作为一名用户会有什么感受。

因此,每个人都试图做到的是,作为应用程序,您不应该期望将手机切换到飞行模式。只有用户才能做到这一点。

现在,如果用户更改为飞行模式,系统会广播一个 Intent,您的应用程序应该捕获该广播 Intent 并对其进行操作。采取行动意味着很多事情,其中​​包括更改您的用户界面。

即使您找到某种方法来实现今天想要做的事情,系统明天也可能会在没有警告的情况下发生变化(因为它是内部 API/意图),并且您的应用程序将无法运行。从技术上讲,它可能无法在除了你手中的设备之外的任何其他设备上运行,你知道整个 API 合约都是垃圾。

As a legal Android application, you cannot do any of what you are trying to do, including moving the phone into the airplane mode, disabling radios, turning off sound or anything of that sort.

Imagine how you, a user, would feel if some arbitrary application turned off your phone radio and you miss that call which informs you that you won a million dollars.

So what everyone is trying to get to, is that you as an application should not expect to move the phone into airplane mode. Only the user can do that.

Now if the user changes into the airplane mode, the system broadcasts an Intent, your application should catch that broadcast Intent and act on it. Acting on it means a variety of things, which include changing your UI.

Even if you find someway to achieve what you are trying to do today, the system can change tomorrow without warning (since it's an internal API/Intent) and your application will not function. It might, technically, not function on any other device except the one that you hold in your hand, you know the whole API contracts crap.

巴黎夜雨 2025-01-02 11:23:40

正如您所说,答案在文档中:

“这是一个受保护的意图,只能由系统发送。”

Intent 文档参考

它可能在模拟器上工作,因为模拟器不是它不是一个安全设备,可以让您执行通常无法执行的操作(例如浏览文件系统)。另外,您没有提到您尝试使用的 Android API 级别。如果这是当前的情况,谷歌可能会收紧限制。

As you stated, the answer is in the documentation:

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

Intent Documentation Reference

It may work on an emulator because the emulator isn't a secured device and lets you do things (like browse the file system) that you wouldn't normally be able to do. Also, you didn't mention what API level of Android you are trying to use. If it is something current, Google might have tightened down the restriction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文