我添加了AsyncTask来将网络操作卸载到后台线程。我需要确保UI操作位于UI线程上,因此我想在Activity中使用runOnUiThread()。
谢谢你的帮助
WifiApManager
public class WifiApManager { private final WifiManager mWifiManager; public WifiApManager(Context context) { mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); } public boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) { try { if (enabled) { // disable WiFi in any case mWifiManager.setWifiEnabled(false); } Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class); return (Boolean) method.invoke(mWifiManager, wifiConfig, enabled); } catch (Exception e) { Log.e(this.getClass().toString(), "wifi", e); return false; } } public WIFI_AP_STATE getWifiApState() { try { Method method = mWifiManager.getClass().getMethod("getWifiApState"); int tmp = ((Integer)method.invoke(mWifiManager)); // Fix for Android 4 if (tmp > 10) { tmp = tmp - 10; } return WIFI_AP_STATE.class.getEnumConstants()[tmp]; } catch (Exception e) { Log.e(this.getClass().toString(), "wifi", e); return WIFI_AP_STATE.WIFI_AP_STATE_FAILED; } } public boolean isWifiApEnabled() { return getWifiApState() == WIFI_AP_STATE.WIFI_AP_STATE_ENABLED; } public WifiConfiguration getWifiApConfiguration() { try { Method method = mWifiManager.getClass().getMethod("getWifiApConfiguration"); return (WifiConfiguration) method.invoke(mWifiManager); } catch (Exception e) { Log.e(this.getClass().toString(), "wifi", e); return null; } } public boolean setWifiApConfiguration(WifiConfiguration wifiConfig) { try { Method method = mWifiManager.getClass().getMethod("setWifiApConfiguration", WifiConfiguration.class); return (Boolean) method.invoke(mWifiManager, wifiConfig); } catch (Exception e) { Log.e(this.getClass().toString(), "wifi", e); return false; } } public ArrayList<ClientScanResult> getClientList(boolean onlyReachables) { return getClientList(onlyReachables, 10); } public ArrayList<ClientScanResult> getClientList(boolean onlyReachables, int reachableTimeout) { BufferedReader br = null; ArrayList<ClientScanResult> result = null; try { result = new ArrayList<ClientScanResult>(); br = new BufferedReader(new FileReader("/proc/net/arp")); String line; while ((line = br.readLine()) != null) { String[] splitted = line.split(" +"); if ((splitted != null) && (splitted.length >= 4)) { // Basic sanity check String mac = splitted[3]; if (mac.matches("..:..:..:..:..:..")) { boolean isReachable = InetAddress.getByName(splitted[0]).isReachable(reachableTimeout); if (!onlyReachables || isReachable) { result.add(new ClientScanResult(splitted[0], splitted[3], splitted[5], isReachable)); } } } } } catch (Exception e) { Log.e(LOGTAG, e.toString()); } finally { try { br.close(); } catch (IOException e) { Log.e(LOGTAG, e.toString()); } } return result; } }
connect.java
public class connect extends Activity{ WifiApManager wifiApManager; TextView tv; Button scan; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.connect); tv =(TextView) findViewById(R.id.iptv); new scan().execute(); } public class scan extends AsyncTask<String, Integer, String> { public Object WIFI_SERVICE; @Override protected void onProgressUpdate(Integer...integers) { ArrayList<ClientScanResult> clients = wifiApManager.getClientList(false); tv.setText("WifiApState: " + wifiApManager.getWifiApState() + "\n\n"); tv.append("Clients: \n"); for (ClientScanResult clientScanResult : clients) { tv.append("####################\n"); tv.append("IpAddr: " + clientScanResult.getIpAddr() + "\n"); tv.append("Device: " + clientScanResult.getDevice() + "\n"); tv.append("HWAddr: " + clientScanResult.getHWAddr() + "\n"); tv.append("isReachable: " + clientScanResult.isReachable()+ "\n"); } } @Override protected void onPostExecute(String result){ tv.setText(result); } @Override protected String doInBackground(String... params) { wifiApManager = new WifiApManager(this); // the above line shows a Error return null; } } }
编辑
我想在TextView中显示已处理的文本
class scan extends AsyncTask<String, Void, Void> { public Context context; ArrayList<ClientScanResult> clients; public scan(Context c) // constructor to take Context { context = c; // Initialize your Context variable } protected Void doInBackground(String... params) { wifiApManager = new WifiApManager(context); // use the variable here clients = wifiApManager.getClientList(false); return null; } } protected void onPostExecute(Void result){ ArrayList<ClientScanResult> clients; tv.setText("WifiApState: " + wifiApManager.getWifiApState() + "\n\n"); tv.append("Clients: \n"); for (ClientScanResult clientScanResult : clients)//showin error in clients { tv.append("####################\n"); tv.append("IpAddr: " + clientScanResult.getIpAddr() + "\n"); tv.append("Device: " + clientScanResult.getDevice() + "\n"); tv.append("HWAddr: " + clientScanResult.getHWAddr() + "\n"); tv.append("isReachable: " + clientScanResult.isReachable()+ "\n"); } } }
啊! 没有!!!除以外, 每种 方法都AsyncTask可以运行。您也可以通过从进行调用来进行网络操作并更新in 或in 。UI Thread``doInBackground()``doInBackground()``UI``onPostExecute()``onProgressUpdate()``publishProgress()``doInBackground()
AsyncTask
UI Thread``doInBackground()``doInBackground()``UI``onPostExecute()``onProgressUpdate()``publishProgress()``doInBackground()
请勿runOnUiThread()与一起使用AsyncTask。至少对于我来说,没有理由使用它,AsyncTask因为它具有已经在上运行的方法UI Thread。我从未见过它会做任何事情,但会带来麻烦。
runOnUiThread()
UI Thread
您可以致电publishProgress()您loop,并更新您TextView在onProgressUpdate()或添加值的ArrayList中和更新onProgressUpdate()。
publishProgress()
loop
TextView
onProgressUpdate()
ArrayList
请多次阅读文档。AsyncTask刚开始时有点棘手,但是一旦您了解了它的功能,那么它可能就是一件美丽的事情。
创建您的实例AsyncTask并将其传递Activity Context给它
Activity Context
Scan myScan = new scan(this); // pass the context to the constructor myScan.execute();
然后在中创建一个构造函数,AsyncTask并接受Context。
Context
public class scan extends AsyncTask<String, Integer, String> { public Object WIFI_SERVICE; public Context context; // Context variable public scan(Context c) // constructor to take Context { context = c; // intialize your Context variable }
现在使用该变量
@Override protected String doInBackground(String... params) { wifiApManager = new WifiApManager(context); // use the variable here return null; }
另一个编辑
class scan extends AsyncTask<String, Void, Void> { ArrayList<ClientScanResult> clients; Context context; ... then initialize your `clients` in `doInBackground()` clients = wifiApManager.getClientList(false);
改变onPostExecute()为不接受任何东西
onPostExecute()
protected void onPostExecute(Void result){
然后将更新其中的代码TextView放入其中。