简体   繁体   English

在Android棉花糖上启动新的“扫描”设置活动

[英]Launch the new “Scanning” settings activity on Android Marshmallow

The goal is to programmatically get to the Settings activity in Android that controls the option that used to be "Wi-Fi scanning always available" in the Advanced Wi-Fi settings prior to Android M. I used to get to this activity as follows: 我们的目标是通过编程方式进入Android中的“设置”活动,该活动控制着Android M之前的高级Wi-Fi设置中曾经是“始终可用的Wi-Fi扫描”的选项。

startActivity(new Intent(Settings.ACTION_WIFI_IP_SETTINGS));

The setting has now moved under Settings -> Location -> Scanning (overflow menu). 现在,该设置已移至“设置”->“位置”->“扫描”(溢出菜单)下。 There doesn't seem to be a constant defined under Settings for the new page. 在新页面的“设置”下似乎没有定义常量。 How can I get the user to this new screen from my app? 如何使用户从我的应用程序转到这个新屏幕?

I found the relevant preference fragment at com.android.settings.location.ScanningSettings.java. 我在com.android.settings.location.ScanningSettings.java中找到了相关的首选项片段。 I think it's used by SettingsActivity to launch this preference panel? 我认为SettingsActivity使用它来启动此首选项面板吗?

https://github.com/android/platform_packages_apps_settings/blob/master/src/com/android/settings/location/ScanningSettings.java https://github.com/android/platform_packages_apps_settings/blob/master/src/com/android/settings/location/ScanningSettings.java

How can I get the user to this new screen from my app? 如何使用户从我的应用程序转到这个新屏幕?

Unfortunately you can't, there isn't an action that open that fragment. 不幸的是,您不能打开该片段。

Your best bet is to open the Location settings with this code: 最好的选择是使用以下代码打开“位置”设置:

startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));

and let the user open the overflow menu and select Scanning . 并让用户打开溢出菜单并选择“ Scanning

Another possible solution could be prompt the request directly from your app: 另一个可能的解决方案是直接从您的应用程序提示请求:

在此处输入图片说明

First check the state of Wi-Fi scanning, than show the dialog if it's disabled: 首先检查Wi-Fi扫描的状态,然后显示对话框(如果已禁用):

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    if (wifiManager.isScanAlwaysAvailable()) {
        Toast.makeText(MainActivity.this, "Wi-Fi scanning is on", Toast.LENGTH_SHORT).show();
    } else {
        startActivityForResult(new Intent(WifiManager.ACTION_REQUEST_SCAN_ALWAYS_AVAILABLE), MY_REQUEST_CODE);
    }
}

Then in onActivityResult check what user has chosen: 然后在onActivityResult检查用户选择了什么:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == MY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Toast.makeText(this, "User has enabled Wi-Fi scanning", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "User has denied Wi-Fi scanning", Toast.LENGTH_SHORT).show();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM