SCAN_RESULTS_AVAILABLE_ACTION:在Android 7.0上未调用BroadcastReceiver#onReceive

时间:2017-04-20 10:16:04

标签: android android-broadcastreceiver android-7.0-nougat

我正在为Android开发应用程序,我必须扫描可用的WiFi LAN。

我正在测试该应用,它适用于Android设备从4.4到6.x,但是在Nougat上(事实上我已经在2个不同的设备上测试过,华为和HTC,用Android测试7)我的onReceive的{​​{1}}永远不会被调用。

如果我尝试使用BroadcastReceiver我即使在Android 7上也可以接收此操作。

我检查过这些变化'在Nougat的文档,但我没有发现这一点。为什么它不适用于Nougat?我需要添加/更改什么?

这是我应用的精简版。

AndroidManifest.xml 中,我已添加:

WIFI_STATE_CHANGED_ACTION

MainActivity.java

    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

WiFiConnection.java

public class MainActivity extends AppCompatActivity {
private ListView _listSSID;
private ArrayAdapter<String> _adapter;
private WiFiConnection _wifiConnection = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
protected void onStart() {
    super.onStart();
    _wifiConnection = new WiFiConnection(this);
    _adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
            android.R.id.text1, _wifiConnection.getBestSSIDs());
    _listSSID = (ListView) findViewById(R.id.listSSID);
    _listSSID.setAdapter(_adapter);
    _listSSID.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String ssid = _adapter.getItem(position);
            Log.d("OnClick", ssid);
        }
    });
    startScan();
}

@Override
protected void onStop() {
    super.onStop();

    _wifiConnection.stopScan();
    unregisterReceiver(_wifiScanReceiver);
}

void startScan() {
    checkPermission(this);

    registerReceiver(_wifiScanReceiver,
            new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    Thread t = new Thread(_wifiConnection.getWifiScanner());
    t.start();
}

private final BroadcastReceiver _wifiScanReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context c, Intent intent) {
        if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
            if (_wifiConnection != null && _wifiConnection.isWifiEnabled()) {
                _wifiConnection.updateScanData("No WLANs found");
            }
            _adapter.notifyDataSetChanged();
        }
    }
};

static final int MY_PERMISSIONS_REQUEST = 1042;

private static final String PERMISSIONS_TAG = "PERMISSION";

@TargetApi(Build.VERSION_CODES.M)
public static boolean checkPermission(Activity activity) {
    boolean permission = true;
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        permission = activity.checkSelfPermission(Manifest.permission.ACCESS_WIFI_STATE) == PackageManager.PERMISSION_GRANTED;
        Log.d(PERMISSIONS_TAG, "ACCESS_WIFI_STATE: " + permission);
        permission = activity.checkSelfPermission(Manifest.permission.CHANGE_WIFI_STATE) == PackageManager.PERMISSION_GRANTED;
        Log.d(PERMISSIONS_TAG, "CHANGE_WIFI_STATE: " + permission);
        permission = activity.checkSelfPermission(Manifest.permission.CHANGE_WIFI_MULTICAST_STATE) == PackageManager.PERMISSION_GRANTED;
        Log.d(PERMISSIONS_TAG, "CHANGE_WIFI_MULTICAST_STATE: " + permission);

        List<String> requiringList = new ArrayList<>();
        permission = activity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
        Log.d(PERMISSIONS_TAG, "WRITE_EXTERNAL_STORAGE: " + permission);
        if (!permission) {
            requiringList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }

        permission = activity.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
        Log.d(PERMISSIONS_TAG, "ACCESS_COARSE_LOCATION: " + permission);
        if (!permission) {
            requiringList.add(Manifest.permission.ACCESS_COARSE_LOCATION);
        }

        if (requiringList.size() > 0) {
            String[] stringArray = requiringList.toArray(new String[0]);
            activity.requestPermissions(stringArray, MY_PERMISSIONS_REQUEST);
        }
    }
    return permission;
}
}

2 个答案:

答案 0 :(得分:0)

似乎有必要打开GPS以在Android 7.0上接收SCAN_RESULTS_AVAILABLE_ACTION。

答案 1 :(得分:0)

@TargetApi(Build.VERSION_CODES.M)删除它将为Android M执行其他明智的写入更大或相等的M版本代码。

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M){
    // Do something for M and above versions
} else{