You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android平台Kivy应用运行随机时长后冻结致重启问题求助

问题描述
  • 开发的Android平台Kivy应用会在随机时长后冻结自身UI及系统UI,最终导致手机重启
  • 已采用线程机制确保UI更新在主线程执行,但冻结问题仍未解决
  • 测试结果:使用小缓冲区时应用可运行约12分钟,使用5倍大小的大缓冲区时可运行约37分钟,暂未测试蓝牙关闭状态下的情况
  • 冻结前现象:UI更新间隔会从预设的1秒逐渐延长至2、3、10秒,最终完全无响应
  • 线程中的accumulator方法与主线程的signaling方法耗时均约0.01秒,后者负责生成图表图片并更新UI
程序代码模板
## 应用类

class BiteSignal(App):
    
    def __init__(self, use_bluetooth, device_name, calibration_time_minutes, signal_sigmas, readings_per_minute):

        App.__init__(self)

        self.use_bluetooth = use_bluetooth

        if self.use_bluetooth:
            
            self.BluetoothAdapter = autoclass('android.bluetooth.BluetoothAdapter')
            self.BluetoothDevice = autoclass('android.bluetooth.BluetoothDevice')
            self.BluetoothSocket = autoclass('android.bluetooth.BluetoothSocket')
            self.InputStreamReader = autoclass('java.io.InputStreamReader')
            self.BufferedReader = autoclass('java.io.BufferedReader')
            self.UUID = autoclass('java.util.UUID')
            self.StringBuilder = autoclass('java.lang.StringBuilder')
        
        ### 其他初始化代码
    
    
    def connect_bt(self):
        
        status_label.text = 'Пытаюсь соединиться с блютусом...'

        paired_devices = self.BluetoothAdapter.getDefaultAdapter().getBondedDevices().toArray()
        
        for self.device in paired_devices:
            
            if self.device.getName() == self.device_name:
                
                try:
                    
                    self.socket = self.device.createRfcommSocketToServiceRecord(self.UUID.fromString('00001101-0000-1000-8000-00805F9B34FB'))

                    if not self.socket.connected:
                    
                        self.socket.connect()

                        return True

                except Exception as e:
                    
                    status_label.text = 'error: ' + str(e)
                    
                    return False

    
    def get_data(self):

        if not self.use_bluetooth: # 使用随机数据
            
            dat = random.randint(128, 1024)

            if dat > 1013:

                dat = random.randint(1024, 512000)

            return math.log(dat, 2)

        else: # 使用Arduino传来的数据
            
            if not self.socket.connected:
                
                self.connect_bt()
            
            try:
    
                bufferedreader = self.BufferedReader(self.InputStreamReader(self.socket.getInputStream(), 'US-ASCII'))
                
                input_read = bufferedreader.readLine().strip().rstrip()
    
                if input_read.isdigit():
    
                    if int(input_read) > 0:
                        
                        input_read_int = int(input_read)
                        
                        return math.log(input_read_int, 2)
    
                    else:
    
                        return None
                
                else:
                    
                    return None
            
            except Exception as e:
                
                status_label.text = 'Data error: ' + str(e)
    
                return None

    
    def buffer_start(self):

        # 数据缓冲区
        
        self.accel_buffer = []
    
    
    def accumulator(self):

        ## 线程停止机制

        thr = threading.currentThread()
        
        while getattr(thr, "do_run", True):
            
            dat = self.get_data()
            
            if dat is not None:
                
                ## 添加到缓冲区并执行统计操作
    
            self.signaling() ### 主线程中更新UI

            time.sleep(0.1) ### 数据源每秒产生10条数据
    
    
    @mainthread # 用于管理UI的装饰器
    def signaling(self):

        ### 处理数据、应用统计、绘制图表,并更新UI内容


    
    def start_restart(self, instance): ## 点击Start按钮时调用

        if self.use_bluetooth:
            self.connect_bt()

        self.buffer_start()

        try:
            print('尝试停止现有线程')
            self.data_accumulator_thread.do_run = False
        except Exception as e:
            print(e)

        try:
            print('初始化线程')
            self.data_accumulator_thread = threading.Thread(target = self.accumulator)
            print('启动线程')
            self.data_accumulator_thread.start()
        except Exception as e:
            print(e)
        
        action_button.text = 'Restart'
    
    
    def build(self):
        
        ### 构建UI:盒布局、标签、按钮等
        
        return box_root
        

## 运行应用
if __name__ == '__main__':
    
    BS = BiteSignal(
        use_bluetooth = True,
        device_name = 'HC-06',
        calibration_time_minutes = 1,
        signal_sigmas = 5,
        readings_per_minute = 600
        )
    
    BS.run()

内容的提问来源于stack exchange,提问作者alexeymosco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 16:09:53