MainActivity.java
package com.blogspot.android_er.androidscannetworkinterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
public class MainActivity extends AppCompatActivity {
private Button btnScan;
private ListView listViewNI;
ArrayList<String> niList;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnScan = (Button)findViewById(R.id.scan);
listViewNI = (ListView)findViewById(R.id.listviewni);
niList = new ArrayList();
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, niList);
listViewNI.setAdapter(adapter);
btnScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new ScanNITask().execute();
}
});
}
private class ScanNITask extends AsyncTask<Void, String, Void> {
@Override
protected void onPreExecute() {
niList.clear();
adapter.notifyDataSetInvalidated();
}
@Override
protected Void doInBackground(Void... params) {
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface thisInterface = networkInterfaces.nextElement();
Enumeration<InetAddress> inetAddresses = thisInterface.getInetAddresses();
if (inetAddresses.hasMoreElements()) {
String niInfo = thisInterface.getDisplayName() + " ";
while (inetAddresses.hasMoreElements()) {
InetAddress thisAddress = inetAddresses.nextElement();
niInfo += "--- ";
niInfo += "Address: " + thisAddress.getAddress() + " ";
niInfo += "CanonicalHostName: " + thisAddress.getCanonicalHostName() + " ";
niInfo += "HostAddress: " + thisAddress.getHostAddress() + " ";
niInfo += "HostName: " + thisAddress.getHostName() + " ";
}
publishProgress(niInfo);
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
niList.add(values[0]);
adapter.notifyDataSetInvalidated();
}
@Override
protected void onPostExecute(Void aVoid) {
Toast.makeText(MainActivity.this, "Done", Toast.LENGTH_LONG).show();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android_layout_width="match_parent"
android_layout_height="match_parent"
android_padding="16dp"
android_orientation="vertical"
tools_context=".MainActivity">
<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_gravity="center_horizontal"
android_autoLink="web"
android_text="http://android-er.blogspot.com/"
android_textStyle="bold" />
<Button
android_id="@+id/scan"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_text="Scan"/>
<ListView
android_id="@+id/listviewni"
android_layout_width="match_parent"
android_layout_height="wrap_content"/>
</LinearLayout>
Permission of "android.permission.INTERNET" is needed in AndroidManifest.xml
0 comments:
Post a Comment