javaandroidinheritanceandroid-wifiwifimanager

Can't turn-on WiFi by a class object


I know that I can turn-On WiFi in the OnCreate() by a WifiManager object. But my project demands an Object-Oriented approach rather than simply using a WiFi functionality. So I made a class:

public class WiFiSenderBase extends Activity implements Sender
{  
    public static WifiManager wifi;
    
    WiFiSenderBase()
    {
        wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    }
            
    @Override
    public boolean TurnOn()
    {
        if(!wifi.isWifiEnabled())
        {
            wifi.setWifiEnabled(true);
            return true;
        }
        return false;
    }
}

now in the MainActivity class and onCreate() :

public class MainActivity extends Activity
{
    ToggleButton t;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        t = (Button)findViewById(R.id.Button1_ref);
        final WiFiSenderBase wifi = new WiFiSenderBase();
        t.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v) 
                {
                    wifi.TurnOn();
                }
            });
    }
}

now, the problem is that the app crashes on onCreate() itself and makes a toast

Unfortunately, App has stopped

I think that there is a problem in instantiation of WiFiSenderBase class in the MainActivity, as WiFiSenderBase also extends Activity class, but If I do not extend Activity in WiFiSenderBase then I do not get to use the getSystemService().

p.s. using Eclipse mars, and all the necessary imports have been made


Solution

  • I found the solution. I turn-On WiFi in the OnCreate() by a WifiManager object. And pass it to the constructor of WiFiSenderBase.:

    public class WiFiSenderBase implements Sender
    {  
        public static WifiManager wifi;
    
        WiFiSenderBase(WifiManager man)
        {
            wifi = man;
        }
    
        @Override
        public boolean TurnOn()
        {
            if(!wifi.isWifiEnabled())
            {
                wifi.setWifiEnabled(true);
                return true;
            }
            return false;
        }
    }
    

    now in the MainActivity class and onCreate() :

    public class MainActivity extends Activity
    {
        public WifiManager wifiman;
        ToggleButton t;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            wifiman = (WifiManager) getSystemService(Context.WIFI_SERVICE);
            final WiFiSenderBase wifi = new WiFiSenderBase(wifiman);
            t = (ToggleButton)findViewById(R.id.toggleButton1_ref);
            t.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v) 
                    {
                        wifi.TurnOn();
                    }
                });
        }
    }
    

    now, the problem is solved.

    p.s. using eclipse mars, and all the necessary imports have been made