1

我一直在尝试创建一个 Android 应用程序来检查手机的 wifi 是否打开或关闭。我想要做的是,如果 WiFi 开启,那么它会显示一个白色的 BoxLayout,如果它没有开启,那么它会显示一个弹出窗口。我是 Android 开发的新手,我迷上了 Kivy,因为它的简单性和我对 Python 的热爱。我尝试关注 Pyjnius 文档,但无法运行该应用程序。所以我会在这里发布代码:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.lang import Builder
from jnius import autoclass

Builder.load_string('''
<WiFiENABLED>
    canvas.before:
        Color:
            rgb: 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size

<WiFiDISABLED>
    BoxLayout:
        orientation: 'vertical'
        Label:
            text:"Wifi is turned off"
        Button:
            text:"OK"

<Box>
    Label:
        text: "WiFi Tester"
''')

class WiFiENABLED(BoxLayout):
    def wfenabled(self):
        pass

class WiFiDISABLED(Popup):
    def wfdisabled(self):
        pass

class WiFiStatus():
    def WiFi(self):
        WiFi_Status = autoclass('android.net.wifi.WiFiManager')
        WiFi = WiFi_Status.getSystemService(Context.WIFI_SERVICE)

        if WiFi.getWifiState() == WiFi.WIFI_STATE_ENABLED:
            return WiFiENABLED()

        elif WiFi.getWifiState() == WiFi.WIFI_STATE_DISABLED:
            return WiFiDISABLED()

class Box(BoxLayout):
    def b(self):
        return WiFiStatus()

class WF(App):
    def build(self):
        return Box()

if __name__ == "__main__":
    WF().run()

我应该对 builder.spec 文件中的权限进行任何更改吗?截至目前,它只是互联网。

4

2 回答 2

3

在我的项目中,我使用了这个功能(用于 WiFi 和移动互联网):

from kivy.utils import platform
from jnius import autoclass


def check_connectivity():
    if platform != 'android':
        raise Exception('run me on android device')

    Activity = autoclass('android.app.Activity')
    PythonActivity = autoclass('org.renpy.android.PythonActivity')
    activity = PythonActivity.mActivity
    ConnectivityManager = autoclass('android.net.ConnectivityManager')

    con_mgr = activity.getSystemService(Activity.CONNECTIVITY_SERVICE)

    conn = con_mgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting()
    if conn:
        return True
    else:
        conn = con_mgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting()
        if conn:
            return True
        else:
            return False

True如果互联网可用,则返回,否则返回False

于 2016-03-11T15:25:25.367 回答
0

这对我来说很简单:

from jnius import autoclass

activity = autoclass('org.kivy.android.PythonActivity').mActivity
Context = autoclass('android.content.Context')
ConnectivityManager = autoclass('android.net.ConnectivityManager')

con_mgr = activity.getSystemService(Context.CONNECTIVITY_SERVICE)
conn = con_mgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting()

    if conn:
        print("------------------True")
    else:
        conn = con_mgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting()
        if conn:
            print("------------------True")
        else:
            print("------------------False")
于 2020-10-17T14:59:24.997 回答