public class WifiInfoReceiver extends BroadcastReceiver{ PRivate ScanEnd scanEnd; public static final int SCAN_END_TAG=0; public static final int SCAN_FAIL_TAG=-1; @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)){ // WIFI扫描结束 scanEnd.scanEnd(SCAN_END_TAG); }else { scanEnd.scanEnd(SCAN_FAIL_TAG); } } public interface ScanEnd { public void scanEnd(int tag); } public void scanEndListen(ScanEnd scanEnd) { this.scanEnd = scanEnd; }}2、Activity中接收数据
public class LineActivity extends BaseActivity implements WifiInfoReceiver.ScanEnd{ private static final String TAG = "LineActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_line); initView(); } private void initView() { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); WifiInfoReceiver receiver = new WifiInfoReceiver(); registerReceiver(receiver, intentFilter); receiver.scanEndListen(this); WifiManage.getIntance(this).startScan(); } @Override public void scanEnd(int tag) { if (tag==WifiInfoReceiver.SCAN_END_TAG){ Log.i(TAG, "scanEnd: success");//扫描完成 }else { Log.i(TAG, "scanEnd: fail");// 扫描失败 } }}
新闻热点
疑难解答