【问题标题】:intent-filter in the manifest does not launch my activity清单中的意图过滤器不会启动我的活动
【发布时间】:2011-06-24 03:14:56
【问题描述】:

我正在“设置”应用中创建一个“新应用”来管理蓝牙。我有下一个问题:我希望在框架发送下一个 android 意图时启动我的活动:android.bluetooth.device.action.PAIRING_REQUEST

因此,我将其添加到设置应用程序的 AndroidManifest.xml 中:

<activity android:name=".mybt.MyBluetoohSettings">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

问题在于,一旦框架发送了该意图......我的应用程序没有启动!

我也尝试过仅使用单个意图过滤器(使用 MAIN 和 PAIRING_REQUEST 意图)

理论上这应该可行,对吧?我究竟做错了什么?有什么建议吗?

提前致谢!

【问题讨论】:

    标签: android android-manifest intentfilter


    【解决方案1】:

    蓝牙配对请求需要由 Receiver(从 BroadcastReceiver 扩展)而不是 Activity 来处理。因此,您的清单应该包含一个元素,在 &lt;application&gt; 内,看起来像这样:

        <receiver android:name=".MyReceiver">
            <intent-filter>
                <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
            </intent-filter>
        </receiver>
    

    在你的应用程序的某个地方,你会有一个名为 MyReceiver 的类:

    public class MyReceiver extends BroadcastReceiver {
    
       public void onReceive(Context context, Intent intent) {
    
          //
          // Handle the pairing request
          //
       }
    }
    

    【讨论】:

    • 是的,我在原来的蓝牙应用中看到过。但我不明白他们为什么这样做。为什么不能使用活动?你能解释一下吗?我的问题是,现在,我的活动有一个带有意图过滤器的广播接收器对象。所以当我的活动被实例化时,接收者被注册,我可以处理意图。但是,如果不是...我希望 android 启动它。对这种架构有什么建议吗?谢谢!
    • 这只是Android中广播的本质;因为可能有多个应用程序对配对请求感兴趣,所以使用广播接收器。但是,行为应该是相同的;如果您在清单中注册了广播接收器,Android 将启动您的应用程序并在事件发生时调用广播接收器,即使应用程序当时没有运行。请注意,如果应用程序崩溃或被强制关闭,这将不起作用;如果这是所需的行为,您需要创建一个粘性服务,Android 将尝试重新启动。
    猜你喜欢
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多