用于检查是否启用了wifi的Android应用

时间:2016-03-11 01:03:56

标签: android python-2.7 kivy pyjnius

我一直在尝试创建一个Android应用来检查手机的wifi是打开还是关闭。 我想要做的是,如果WiFi打开,则显示一个白色的BoxLayout,如果它没有打开,则显示一个Popup。 我是Android开发的新手,因为它的简单性和对Python的热爱,我迷上了Kivy。 我试过跟随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文件中的权限进行任何更改吗?截至目前,它只是互联网。

2 个答案:

答案 0 :(得分:2)

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

function yesnoCheck() {

  if($('#yesCheck').is(':checked')) {
   $('#ifYes').addClass('yarp');
   $('#businessname').attr('required');​​​​​
  } else {
   $('#ifYes').removeClass('yarp');
   $('#businessname').removeAttr('required');​​​​​
  }

}

如果互联网可用,则返回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

答案 1 :(得分: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")