1

我的 wifi 广播接收器有问题。它不接收任何东西,永远不会调用 onReceive。这是我的代码:

public final class WifiChangeReceiver extends BroadcastReceiver {

    boolean portableHotspot = true;

    @Override
    public void onReceive(final Context context, Intent intent) {

        boolean alreadyPresent = false;

        String action  = intent.getAction();
        if(action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
            WifiManager w = (WifiManager) context
                    .getSystemService(Context.WIFI_SERVICE);
            List<ScanResult> l = w.getScanResults();

            myApp myApp = (myApp)context.getApplicationContext();
            List<ScanResult> previousScan = new ArrayList<ScanResult>();
            previousScan = myApp.getPreviousScan();

            System.out.print("previousScan " + previousScan + "\n");

            for (ScanResult r : l) {
                if(r.SSID.equals("\""+"TinyBox"+"\"")) {
                    if(previousScan!=null) {
                        for (ScanResult previousScanElement : previousScan) {
                            if(previousScanElement.SSID.contains("\""+"TinyBox"+"\"")) {
                                alreadyPresent = true;
                                break;
                            }
                        }
                    }
                    if (!alreadyPresent) {

                        final Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {
                          @Override
                          public void run() {

                              Methods.connectToNetwork(context);

                          }
                        }, 5000);

                        alreadyPresent = false;
                    }
                }
            }

            myApp.setPreviousScan(l);


            ConnectivityManager conMgr =  (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo i = conMgr.getActiveNetworkInfo();

            //Se non connesso a nessuna rete, allora tiro su TinyBox.
            if (i == null) {

                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                i = conMgr.getActiveNetworkInfo();
                if (i == null) {
                    boolean portableHotspot = false;

                    final SQLiteDatabase db = DatabaseManager.getInstance(context).getWritableDatabase();

                    Cursor c = HotSpotActivation.getHotSpotState(db);

                    if (c.getCount() > 0) {
                        String activation = c.getString(c.getColumnIndex(HotSpotActivation.CHECK_STATE));

                        if (activation.equals("ON")) {
                            portableHotspot = true;
                        }
                        else {
                            portableHotspot = false;
                        }
                    }

                    if (portableHotspot) {
                        Methods.setWirelessHotSpot(context.getApplicationContext());
                    }
                }
            }
            else {
            }

        }
        else if(action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
            int iTemp = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
                    WifiManager.WIFI_STATE_UNKNOWN);
            checkState(iTemp);
        }
        else if(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
            DetailedState state=
                    ((NetworkInfo)intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO)).getDetailedState();
            changeState(state, context);
        }
    }

    private void changeState(DetailedState aState, Context context) {
        if (aState == DetailedState.SCANNING) {
            Log.d("wifiSupplicanState", "SCANNING");
        }
        else if (aState == DetailedState.CONNECTING) {
            Log.d("wifiSupplicanState", "CONNECTING");
        }
        else if(aState == DetailedState.OBTAINING_IPADDR) {
            Log.d("wifiSupplicanState", "OBTAINING_IPADDR");
        }
        else if (aState == DetailedState.CONNECTED) {
            Log.d("wifiSupplicanState", "CONNECTED");
        }
        else if (aState == DetailedState.DISCONNECTING) {
            Log.d("wifiSupplicanState", "DISCONNECTING");
        }
        else if (aState == DetailedState.DISCONNECTED) {

            myApp myApp = (myApp)context.getApplicationContext();

            portableHotspot = myApp.getPortableHotspot();

            if (portableHotspot) {
                Methods.setWirelessHotSpot(context.getApplicationContext());
            }

        }
        else if (aState == DetailedState.FAILED) {
        }
    }

    public void checkState(int aInt) {         
        if (aInt == WifiManager.WIFI_STATE_ENABLING) {
            Log.d("WifiManager", "WIFI_STATE_ENABLING");
        }
        else if (aInt== WifiManager.WIFI_STATE_ENABLED) {
            Log.d("WifiManager", "WIFI_STATE_ENABLED");
        }
        else if (aInt == WifiManager.WIFI_STATE_DISABLING) {
            Log.d("WifiManager", "WIFI_STATE_DISABLING");
        }
        else if (aInt == WifiManager.WIFI_STATE_DISABLED) {
            Log.d("WifiManager", "WIFI_STATE_DISABLED");
        }
    }
}

和清单:

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_SUPERUSER"/>
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
    <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />    
<receiver android:name="sample.broadcastreceivers.WifiChangeReceiver">
    <intent-filter>
        <action android:name="android.net.wifi.WIFI_STATE_CHANGED"></action>
        <action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE"></action>
        <action android:name="android.net.wifi.STATE_CHANGE"></action>
    <action android:name="android.net.wifi.SCAN_RESULTS"></action>
    </intent-filter>
</receiver>

只有当我进入列出网络的 wifi 设置菜单时,它才有效(在接收被调用时),无需推送任何内容,只需打开它。进入该菜单后,我立即开始从广播接收器接收,一切正常,没有任何理由。

问题出在哪里?这就像一个Android错误。我很确定以前有时可以使用相同的代码。谢谢

4

1 回答 1

1

检查您的 AndroidManifest:您声明

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

?

于 2013-06-10T20:44:21.793 回答