【问题标题】:How do I open the Bluetooth Settings Activity programmatically?如何以编程方式打开蓝牙设置活动?
【发布时间】:2012-12-04 09:21:35
【问题描述】:

我想在点击按钮时打开蓝牙设置 像这样看图片

HomeActivity.java

button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                final Intent intent = new Intent(Intent.ACTION_MAIN, null);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);
                final ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.bluetoothSettings");
                intent.setComponent(cn);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity( intent);
            }
        });

【问题讨论】:

标签: android android-intent settings


【解决方案1】:

如果您想打开扫描对话框(不离开您的应用)。

    Intent bluetoothPicker = new Intent("android.bluetooth.devicepicker.action.LAUNCH");
    startActivity(bluetoothPicker);

【讨论】:

    【解决方案2】:

    我认为你应该试试这个更简单的:

    startActivity(new Intent(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS));
    

    【讨论】:

    • 这确实是正确的答案。在 Android 5.0 上测试。
    【解决方案3】:

    也许我错过了什么,但这不是更简单的未来证明解决方案吗?

    Intent intentOpenBluetoothSettings = new Intent();
    intentOpenBluetoothSettings.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS); 
    startActivity(intentOpenBluetoothSettings); 
    

    绝对不可能“删除”其他设置。在手机上只显示一类设置。在平板电脑上,由于一些额外的空间,设置显示在主从布局中,因此平板电脑屏幕的一半以上没有空白空间。这就是 Android 的设计方式,只需编写一个无法更改的应用程序即可。

    正如@zelanix 所建议的那样,清单中的BLUETOOTH_ADMIN 权限是必需的。

    【讨论】:

    • 这对我来说似乎是更好的解决方案。只是一个评论(这适用于两种解决方案),这需要BLUETOOTH_ADMIN 权限。
    • 有没有办法让这个设置窗口从你的应用程序中打开“非模态”?返回应用程序的唯一方法是通过主页按钮旁边的“返回”按钮,而不是像我预期的那样,两个窗口分别出现在应用程序的“任务列表”视图中。 (也许这种“模态”方法是首选的标准技术?抱歉,作为用户和开发人员,我是 Android 的新手)
    • Ted,你有没有想过更好的方法?
    • 这应该不需要 BLUETOOTH_ADMIN 并且 Android 上没有库存——可能是制造商的错误。我向三星报告了这个。
    • 在 Android "Q" 我们得到 Settings.Panel 。 developer.android.com/reference/android/provider/… 它尚不支持蓝牙(仅支持 NFC),但要注意“删除其他设置”并获得蓝牙开/关开关。
    【解决方案4】:

    adb shell am start -a android.settings.BLUETOOTH_SETTINGS

    【讨论】:

    • 问题似乎是关于以编程方式进行,而不是通过命令行。
    【解决方案5】:

    使用

    ComponentName cn = new ComponentName("com.android.settings", 
                       "com.android.settings.bluetooth.BluetoothSettings");
    

    而不是

    final ComponentName cn = new ComponentName("com.android.settings", 
                                  "com.android.settings.bluetoothSettings");
    

    启动蓝牙设置设置

    【讨论】:

    • 但也显示我想要的其他设置只显示蓝牙设置所有其他必须隐藏
    • 我使用的是 android 4.0.3 平板电脑
    • @AndroidNewc:此代码用于启动 BluetoothSettings 设置活动,我认为不可能从设置活动中删除所有其他选项
    • 恕我直言,这可能会因任何新的 android 版本而崩溃
    • 适用于 API 26 (Android 8)。 “setAction”解决方案不适用于 API 26。
    猜你喜欢
    • 2018-05-21
    • 1970-01-01
    • 2019-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    相关资源
    最近更新 更多