【问题标题】:How to handle pausing application in Android written in Kivy-Python?如何处理用 Kivy-Python 编写的 Android 中的暂停应用程序?
【发布时间】:2017-10-30 16:56:01
【问题描述】:

我想问一下如何在kivy Python 编写的Android 中处理暂停应用程序。我有一个使用kivy 用Python 编写的简单应用程序。当它部署到 Android 设备时,它工作正常。但是,如果我按下主页按钮或手机进入睡眠状态,那么当重新打开应用程序时,它仍会像上次暂停前一样显示,但 根本不响应任何触摸..并且在几次之后秒消息显示“应用程序没有响应。您要关闭它吗?”

似乎唯一仍然有效的是应用的kivy.clock.Clock.schedule_interval 函数。 (可以通过动画看到)

deployrun 使用Buildozer 时,log 没有任何错误。内部似乎运行正常,但没有响应用户。

我已尝试添加:

class mainApp(App):    
    def build(self):
        global mainw
        mainw = mainWidget();
        return mainw
    def on_pause(self):
        print('something');
        mainw.clear_widgets();
        return True  ## Changing to False also does not solve the problem
    def on_resume(self):
        mainw.clear_widgets();
        mainw.add_widget(homeWidget());

即使没有这个,或者即使在 on_pauseon_resume 中使用空命令,同样的事情仍然会发生。 print('something') 也不会显示。 这意味着函数on_pause 本身没有被调用

我也尝试过添加(在main.py的顶部):

Config.set('graphics', 'resizable', 0);

Config.set('graphics', 'resizable', 1);

谢谢。

---------

这是部署应用程序时的一些日志:

[INFO]:    Recipe kivy: version "master" requested
[INFO]:    Will compile for the following archs: armeabi-v7a
[INFO]:    Found Android API target in $ANDROIDAPI
[INFO]:    Available Android APIs are (19)
[INFO]:    Requested API target 19 is available, continuing.
[INFO]:    Found NDK dir in $ANDROIDNDK
[INFO]:    Got NDK version from $ANDROIDNDKVER
[INFO]:    Using Google NDK r9c
[INFO]:    Found virtualenv at /usr/local/bin/virtualenv
[INFO]:    Found the following toolchain versions: ['4.6', '4.8', 'clang3.3']
[INFO]:    Picking the latest gcc toolchain, here 4.8
[WARNING]: Missing executable: autoconf is not installed
[WARNING]: Missing executable: automake is not installed
[WARNING]: Missing executable: libtoolize is not installed
[INFO]:    Of the existing distributions, the following meet the given requirements:
[INFO]:     myapp: includes recipes (hostpython2, sdl2_image, sdl2_mixer, sdl2_ttf, python2, numpy, sdl2, six, android, pyjnius, kivy), built for archs (armeabi-v7a)
[INFO]:    myapp has compatible recipes, using this one

运行应用程序时的日志:

--------- beginning of crash
--------- beginning of system
--------- beginning of main
11-01 20:59:38.574 31656 31694 I python  : Initialize Python for Android
11-01 20:59:38.574 31656 31694 I python  : Changing directory to the one provided by ANDROID_ARGUMENT
11-01 20:59:38.574 31656 31694 I python  : /data/user/0/org.test.myapp/files/app
11-01 20:59:38.574 31656 31694 I python  : Preparing to initialize python
11-01 20:59:38.574 31656 31694 I python  : crystax_python does not exist
11-01 20:59:38.581 31656 31694 I python  : Initialized python
11-01 20:59:38.581 31656 31694 I python  : AND: Init threads
11-01 20:59:38.583 31656 31694 I python  : testing python print redirection
11-01 20:59:38.584 31656 31694 I python  : Setting up python from ANDROID_PRIVATE
11-01 20:59:38.586 31656 31694 I python  : ('Android path', ['/data/user/0/org.test.myapp/files/app/lib/python27.zip', '/data/user/0/org.test.myapp/files/app/lib/python2.7/', '/data/user/0/org.test.myapp/files/app/lib/python2.7/lib-dynload/', '/data/user/0/org.test.myapp/files/app/lib/python2.7/site-packages/', '/data/user/0/org.test.myapp/files/app'])
11-01 20:59:38.610 31656 31694 I python  : ('os.environ is', {'ANDROID_APP_PATH': '/data/user/0/org.test.myapp/files/app', 'ANDROID_UNPACK': '/data/user/0/org.test.myapp/files/app', 'EXTERNAL_STORAGE': '/sdcard', 'ANDROID_ENTRYPOINT': 'main.pyo', 'ANDROID_ASSETS': '/system/app', 'PYTHONPATH': '/data/user/0/org.test.myapp/files/app:/data/user/0/org.test.myapp/files/app/lib', 'PYTHONOPTIMIZE': '2', 'ANDROID_ROOT': '/system', 'SYSTEMSERVERCLASSPATH': '/system/framework/services.jar:/system/framework/ethernet-service.jar:/system/framework/wifi-service.jar', 'ANDROID_DATA': '/data', 'PYTHONHOME': '/data/user/0/org.test.myapp/files/app', 'ANDROID_ARGUMENT': '/data/user/0/org.test.myapp/files/app', 'BOOTCLASSPATH': '/system/framework/core-libart.jar:/system/framework/conscrypt.jar:/system/framework/okhttp.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/ims-common.jar:/system/framework/apache-xml.jar:/system/framework/org.apache.http.legacy.boot.jar:/system/framework/dolby_ds.jar:/system/framework/dolby_ds2.jar:/system/framework/mediatek-common.jar:/system/framework/mediatek-framework.jar:/system/framework/mediatek-telephony-common.jar', 'ANDROID_SOCKET_zygote_secondary': '11', 'ANDROID_PROPERTY_WORKSPACE': '9,0', 'ASEC_MOUNTPOINT': '/mnt/asec', 'PATH': '/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin', 'ANDROID_STORAGE': '/storage', 'ANDROID_BOOTLOGO': '1', 'ANDROID_PRIVATE': '/data/user/0/org.test.myapp/files', 'PYTHON_NAME': 'python'})
11-01 20:59:38.610 31656 31694 I python  : ('Android kivy bootstrap done. __name__ is', '__main__')
11-01 20:59:38.659 31656 31694 I python  : ['/data/user/0/org.test.myapp/files/app/lib/python2.7/site-packages', '/data/user/0/org.test.myapp/files/app/lib/site-python']
11-01 20:59:38.659 31656 31694 I python  : AND: Ran string
11-01 20:59:38.659 31656 31694 I python  : Run user program, change dir and execute entrypoint
11-01 20:59:38.813 31656 31694 I python  : [**WARNING**] [Config      ] Older configuration version detected (0 instead of 20)
11-01 20:59:38.814 31656 31694 I python  : [**WARNING**] [Config      ] Upgrading configuration in progress.
11-01 20:59:38.825 31656 31694 I python  : [INFO   ] [Logger      ] Record log in /data/user/0/org.test.myapp/files/app/.kivy/logs/kivy_17-11-01_0.txt
11-01 20:59:38.826 31656 31694 I python  : [INFO   ] [Kivy        ] v1.10.1.dev0, git-Unknown, 20171013
11-01 20:59:38.826 31656 31694 I python  : [INFO   ] [Python      ] v2.7.2 (default, Oct 13 2017, 07:02:15) 
11-01 20:59:38.826 31656 31694 I python  : [GCC 4.8]
11-01 20:59:38.871 31656 31694 I python  : [INFO   ] [Factory     ] 194 symbols loaded
11-01 20:59:39.557 31656 31694 I python  : [INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_gif (img_pil, img_ffpyplayer ignored)
11-01 20:59:39.632 31656 31694 I python  : [INFO   ] [Text        ] Provider: sdl2
11-01 20:59:39.733 31656 31694 I python  : [INFO   ] [OSC         ] using <thread> for socket
11-01 20:59:39.739 31656 31694 I python  : [INFO   ] [Window      ] Provider: sdl2
11-01 20:59:39.769 31656 31694 I python  : [INFO   ] [GL          ] Using the "OpenGL ES 2" graphics system
11-01 20:59:39.770 31656 31694 I python  : [INFO   ] [GL          ] Backend used <gl>
11-01 20:59:39.771 31656 31694 I python  : [INFO   ] [GL          ] OpenGL version <OpenGL ES 3.1 v1.r7p0-02rel0.676427c8bf7f70ce736d81c97bd5cec8>
11-01 20:59:39.771 31656 31694 I python  : [INFO   ] [GL          ] OpenGL vendor <ARM>
11-01 20:59:39.772 31656 31694 I python  : [INFO   ] [GL          ] OpenGL renderer <Mali-T760>
11-01 20:59:39.772 31656 31694 I python  : [INFO   ] [GL          ] OpenGL parsed version: 3, 1
11-01 20:59:39.773 31656 31694 I python  : [INFO   ] [GL          ] Texture max size <8192>
11-01 20:59:39.773 31656 31694 I python  : [INFO   ] [GL          ] Texture max units <16>
11-01 20:59:39.842 31656 31694 I python  : [INFO   ] [Window      ] auto add sdl2 input provider
11-01 20:59:39.844 31656 31694 I python  : [INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
11-01 20:59:40.338 31656 31694 I python  : [**WARNING**] [Base        ] Unknown <android> provider
11-01 20:59:40.338 31656 31694 I python  : [INFO   ] [Base        ] Start application main loop
11-01 20:59:40.343 31656 31694 I python  : [INFO   ] [GL          ] NPOT texture support is available

【问题讨论】:

    标签: android python user-interface kivy


    【解决方案1】:

    这听起来像是来自构建工具中的错误。即使您构建了一个微不足道的应用程序,也会发生这种情况吗?是否会影响其他人构建的 Kivy 应用,例如this one

    【讨论】:

    • 当我第一次使用buildozer 构建时,我简化/最小化了代码,还删除了schedule_interval。那个时候,同样的事情也出现了。我有 Flapping Henry 应用程序,这在我的设备上运行良好。
    • 你的意思是这可能是由buildozer..的某些东西引起的?
    • 是的,这听起来像是您本地构建的问题,但我不确定问题可能是什么。
    • 谢谢。我已经编辑了问题,确实出现了一些警告。其中一些是:autoconfautomake 未安装,还有 Unknown &lt;android&gt; provider
    • 我也意识到on_pause本身没有被触发。
    【解决方案2】:

    我总是按如下方式运行应用程序:

    mainapp = mainApp();
    mainapp.run();
    

    在这个问题之前工作正常。我还检查并看到在使用mainapp.stop() 时,应用程序正在关闭然后重新启动。


    但是,我刚刚发现将其更改为:

    mainapp = mainApp();    
    if __name__ == '__main__':
            mainapp.run();
    

    解决问题。 on_pause 现在在设备上触发。


    但是有什么区别呢??如果对此有一个紧凑的解释,我将不胜感激。谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 2021-07-18
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多