我想创建一个可以扫描连接到我的Lan网络的其他IP的应用程序。这是我的java代码,也许有一个错误导致我的编码没有产生结果。
package com.example.admin.ipscanner3;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
TextView tvWifiState;
TextView tvScanning, tvResult;
ArrayList<InetAddress> inetAddresses;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvWifiState = (TextView)findViewById(R.id.WifiState);
tvScanning = (TextView)findViewById(R.id.Scanning);
tvResult = (TextView)findViewById(R.id.Result);
//To prevent memory leaks on devices prior to Android N,
//retrieve WifiManager with
//getApplicationContext().getSystemService(Context.WIFI_SERVICE),
//instead of getSystemService(Context.WIFI_SERVICE)
WifiManager wifiManager =
(WifiManager)
getApplicationContext().getSystemService(Context.WIFI_SERVICE);
tvWifiState.setText(readtvWifiState(wifiManager));
new ScanTask(tvScanning, tvResult).execute();
}
// "android.permission.ACCESS_WIFI_STATE" is needed
private String readtvWifiState(WifiManager wm){
String result = "";
switch (wm.getWifiState()){
case WifiManager.WIFI_STATE_DISABLED:
result = "WIFI_STATE_DISABLED";
break;
case WifiManager.WIFI_STATE_DISABLING:
result = "WIFI_STATE_DISABLING";
break;
case WifiManager.WIFI_STATE_ENABLED:
result = "WIFI_STATE_ENABLED";
break;
case WifiManager.WIFI_STATE_ENABLING:
result = "WIFI_STATE_ENABLING";
break;
case WifiManager.WIFI_STATE_UNKNOWN:
result = "WIFI_STATE_UNKNOWN";
break;
default:
}
return result;
}
private class ScanTask extends AsyncTask<Void, String, Void> {
TextView tvCurrentScanning, tvScanResullt;
ArrayList<String> canonicalHostNames;
public ScanTask(TextView tvCurrentScanning, TextView tvScanResullt) {
this.tvCurrentScanning = tvCurrentScanning;
this.tvScanResullt = tvScanResullt;
}
@Override
protected void onPostExecute(Void aVoid) {
tvCurrentScanning.setText("Finished.");
tvScanResullt.setText("");
for(int i = 0; i < inetAddresses.size(); i++){
tvScanResullt.append(canonicalHostNames.get(i) + "\n");
}
}
@Override
protected Void doInBackground(Void... voids) {
scanInetAddresses();
return null;
}
@Override
protected void onProgressUpdate(String... values) {
tvCurrentScanning.setText(values[0]);
}
private void scanInetAddresses(){
//May be you have to adjust the timeout
final int timeout = 500;
if(inetAddresses == null){
inetAddresses = new ArrayList<>();
}
inetAddresses.clear();
if(canonicalHostNames == null){
canonicalHostNames = new ArrayList<>();
}
canonicalHostNames.clear();
//For demonstration, scan 192.168.1.xxx only
byte[] ip = {(byte) 183, (byte) 171, (byte) 93, 0};
for (int j = 0; j < 255; j++) {
ip[3] = (byte) j;
try {
InetAddress checkAddress = InetAddress.getByAddress(ip);
publishProgress(checkAddress.getCanonicalHostName());
if (checkAddress.isReachable(timeout)) {
inetAddresses.add(checkAddress);
canonicalHostNames.add(checkAddress.getCanonicalHostName());
}
} catch (UnknownHostException e) {
e.printStackTrace();
publishProgress(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
publishProgress(e.getMessage());
}
}
}
}
}
这是我的界面/布局。我认为这里没有错误。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.admin.ipscanner3.MainActivity">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.069" />
<TextView
android:id="@+id/WifiState"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.144">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scanning: " />
<TextView
android:id="@+id/Scanning"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="italic" />
</LinearLayout>
<TextView
android:id="@+id/Result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.212" />
</android.support.constraint.ConstraintLayout>
我已经添加了这两个权限,以获得访问wifi状态和互联网的权限。
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>