Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何以编程方式打开蓝牙设置活动?_Android_Android Intent_Settings - Fatal编程技术网

Android 如何以编程方式打开蓝牙设置活动?

Android 如何以编程方式打开蓝牙设置活动?,android,android-intent,settings,Android,Android Intent,Settings,我想在单击按钮时打开蓝牙设置 像这样看图片 HomeActivity.java button.setOnClickListener(new OnClickListener() { public void onClick(View v) { final Intent intent = new Intent(Intent.ACTION_MAIN, null); intent.addCategory(Intent

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

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);
            }
        });
使用

而不是

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

要启动BluetoothSettings

也许我错过了一些东西,但这不是一个更简单、经得起未来考验的解决方案吗

Intent intentOpenBluetoothSettings = new Intent();
intentOpenBluetoothSettings.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS); 
startActivity(intentOpenBluetoothSettings); 
绝对不可能“删除”其他设置。在手机上,只显示一类设置。在平板电脑上,由于一些额外的空间,设置显示在主细节布局中,因此平板电脑屏幕的一半以上没有空白。这就是安卓的设计方式,只需编写一个无法更改的应用程序


根据@zelanix的建议,需要清单中的
BLUETOOTH\u ADMIN
权限。

adb shell am start-a android.settings.BLUETOOTH\u settings

我认为您应该尝试更简单的设置:

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

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


在堆栈溢出时签出此答案:但同时显示我想要的其他设置仅显示蓝牙设置所有其他设置必须隐藏并且我正在使用android 4.0.3tablet@AndroidNewc:此代码用于启动Bluetooth设置活动,我认为不可能从设置活动中删除所有其他选项可以在API 26(android 8)上与任何新的android版本一起崩溃。“setAction”解决方案在API 26上不起作用。对我来说,这似乎是更好的解决方案。只是一条评论(这适用于两种解决方案),这需要
BLUETOOTH\u ADMIN
权限。是否有任何方法让此设置窗口从应用程序“非模式”打开?返回应用程序的唯一方法是通过home按钮旁边的“back”(返回)按钮,而不是像我预期的那样在应用程序的“task list”(任务列表)视图中分别显示的两个窗口。(也许这种“模式”方法是首选的、标准的技术?抱歉,作为用户和开发者,我对Android是个新手)Ted,你有没有找到更好的方法?这不需要蓝牙管理,也不需要Android的库存,可能是制造商的错误。我向三星报告了这一点。在Android“Q”中,我们可以获得设置。面板。它还不支持蓝牙(仅支持NFC),但这是一个需要注意的问题,可以“删除其他设置”并获得一个蓝牙开/关开关。问题似乎是以编程方式进行的,而不是通过命令行。这确实是正确的答案。在安卓5.0上测试。
startActivity(new Intent(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS));
    Intent bluetoothPicker = new Intent("android.bluetooth.devicepicker.action.LAUNCH");
    startActivity(bluetoothPicker);