Android Open Source - Simple-Tether Main Activity






From Project

Back to project page Simple-Tether.

License

The source code is released under:

GNU General Public License

If you think the Android project Simple-Tether listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.devlerrinc.simpletether.simpleteather;
//from  ww w. java 2s . co m
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class MainActivity extends Activity {

    boolean wifiOn, dataOn, tetherOn;
    ArrayList info;
    ConnectivityManager connManager;
    NetworkInfo netWifi, netData;

    WifiManager wifiMan;
    Method wifiMethod;

    ProgressDialog pDialog;

    //Global components
    EditText et_info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new checkStatus().execute();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    public void initComponents() {
        Button btn_tether = (Button) findViewById(R.id.main_button_tether);
        et_info = (EditText) findViewById(R.id.main_edittext_info);
        btn_tether.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);

                Method[] methods = wifiManager.getClass().getDeclaredMethods();
                for (Method method : methods) {
                    if (method.getName().equals("tether")) {
                        try {
                            method.invoke(wifiManager, null, true);
                        } catch (Exception ex) {
                            Toast msg;
                            msg = Toast.makeText(getApplicationContext(),ex.getMessage(),Toast.LENGTH_SHORT);
                            msg.show();

                        }
                        break;
                    }
                }

                new checkStatus().execute();
            }

        });
    }

    public void initvariables() {
        wifiOn = false;
        dataOn = false;
        tetherOn = false;
        connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        netWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        netData = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        wifiMan = (WifiManager)getSystemService(Context.WIFI_SERVICE);
        info = new ArrayList();
    }

    public class checkStatus extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
                pDialog = new ProgressDialog(MainActivity.this);
                pDialog.setTitle("Retrieving status");
                pDialog.setMessage("Please wait while your connection status is retrieved.");
                pDialog.setCancelable(false);
                pDialog.show();
                initComponents();
                et_info.setText("");
        }

        @Override
        protected String doInBackground(String... params) {
            initvariables();
            int arraysize = 0;
            if (netWifi.isConnected()) {
                wifiOn = true;
            } else {
                wifiOn = false;
            }
            if (netData.isAvailable() && netData.isConnected()) {
                dataOn = true;
            } else {
                dataOn = false;
            }
            //*** Rewrite as needed
            try
            {
                wifiMethod = wifiMan.getClass().getDeclaredMethod("isWifiApEnabled");
                wifiMethod.setAccessible(true); //in the case of visibility change in future APIs
                tetherOn = (Boolean) wifiMethod.invoke(wifiMan);
            }
            catch (final Throwable ignored)
            {
            }
            //***
            info.add(arraysize++, "Status:");
            info.add(arraysize++, "Wifi: " + (wifiOn ? "On" : "Off").toString());
            info.add(arraysize++, "Data: " + (dataOn ? "On" : "Off" + (wifiOn ? " - This may say \"Off\" because wifi is on, not because data is disabled" : " - Data is disabled or out of service range.")).toString());
            info.add(arraysize, "Tether: " + (tetherOn ? "On" : "Off").toString());
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            for (int a = 0; a < info.size(); a++) {
                et_info.append(info.get(a).toString());
                if (a < info.size()-1)
                    et_info.append("\n---\n");
            }
            if (pDialog != null) {
                pDialog.dismiss();
                pDialog = null;
            }
        }
    }
}




Java Source Code List

com.devlerrinc.simpletether.simpleteather.MainActivity.java