【问题标题】:Android mobile app always get restarted once it is connected or disconnected to bluetooth device一旦连接或断开蓝牙设备,Android 移动应用程序总是会重新启动
【发布时间】:2017-01-11 04:53:55
【问题描述】:

我们有 Android 原生多标签条码应用程序,其中包含用于蓝牙设备输入条码的条码字段。当这个手机吃午饭时,它首先有一个弹出窗口让用户选择标签。问题是每次蓝牙设备与手机连接或断开连接时,应用程序总是会通过此弹出窗口重新启动。当蓝牙设备连接或断开时,我们如何停止手机重新启动?

我正在附加 AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mentor.med.mobile"
    android:versionCode="1"
    android:versionName="1.0.1" >
    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />
        <uses-permission android:name="android.permission.CAMERA" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/med"
        android:label="@string/app_name"
        android:screenOrientation="sensorPortrait"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.pdx.MainActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:windowSoftInputMode="adjustPan"
            android:launchMode="singleTop"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:scheme="http" android:host="pdx.com" android:path="/view" />
            </intent-filter>
        </activity>            
        <activity
            android:name="com.google.zxing.client.android.CaptureActivity"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:windowSoftInputMode="stateAlwaysHidden" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.google.zxing.client.android.SCAN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
</manifest>

【问题讨论】:

  • 请发布您的清单。
  • 嗨,你解决了上面的问题吗,其实我也面临同样的问题

标签: android bluetooth


【解决方案1】:

应用程序可能注册了一个广播接收器,它监听正在连接的蓝牙设备。然后该接收器通过弹出窗口启动 Activity。您有几个选择:

一种解决方案可能是删除清单文件中的条目,该条目将意图过滤器注册到接收器(它可能是一个活动)。但显然,当蓝牙设备连接时,应用程序永远不会自动启动。

或者,您可能希望更改接收器的逻辑,以便在重新打开之前检查活动是否已经打开。一种方法是在 Activity 本身中执行此操作:根据您希望应用程序的行为方式,您可能需要更改 launchMode="singleTop",并使用覆盖方法(例如 isFinishing()onNewIntent())来确定应用程序是否重新启动以及为什么。

【讨论】:

  • 谢谢乔德斯。我尝试了 launchMode="singleTop" 但没有用。我注意到每次蓝牙连接或断开时,应用程序总是这样循环:onPause、onStop、onDestroy、onCreate、onStart、onResume。基本上这个蓝牙设备被用作条码扫描器,它的功能就像键盘一样。我想知道如何让这个应用程序在不经历这些活动周期的情况下保持运行,当蓝牙连接或断开时,就像键盘打开或关闭一样。
  • 我认为您正在尝试与系统作斗争,相反,您的活动应该通过保存状态来处理生命周期事件(onPause() 等),并在onResume() 等上恢复。然后你需要做的就是要做的是使用:1. 保存的状态和 2. 接收到的意图,以确定活动如何或为什么启动/重新启动,并适当地表现。这样做的另一个原因是这是标准做法,以便您的应用程序正确响应方向更改(或其他“配置”更改),因为这些也会导致您的活动经历生命周期更改,这是您无法避免的。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-21
  • 2015-12-18
  • 2012-08-21
  • 1970-01-01
  • 2014-05-05
  • 1970-01-01
相关资源
最近更新 更多