Sunday, December 13, 2015

Android example to list NetworkInterface



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() + "\n";

                        while (inetAddresses.hasMoreElements()) {
                            InetAddress thisAddress = inetAddresses.nextElement();
                            niInfo += "---\n";
                            niInfo += "Address: " + thisAddress.getAddress() + "\n";
                            niInfo += "CanonicalHostName: " + thisAddress.getCanonicalHostName() + "\n";
                            niInfo += "HostAddress: " + thisAddress.getHostAddress() + "\n";
                            niInfo += "HostName: " + thisAddress.getHostName() + "\n";
                        }

                        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
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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

No comments: