How do I get getSystemService from a second .java class file to work in Android Studio?

814 Views Asked by At

I apologise. I'm aware there are many duplicate questions like this, but even after reading through them here I can't get mine to work or properly understand the answer. I'm unable to comment an another post to help explain the answers either because of needing repetition points.


So I'm quite experienced when it comes to JavaScript but I'm still a beginner with Java and learning. I've had some practise with Java on BlueJ a while back.

So lately I've started developing an app with Android Studio with the help of Building Your First App guide, which I had no problem following, learning and understanding.

Now that I'm developing my own app. I keep running into problems when I'm trying to tidy the code snippets I've found by storing them in a second .java file with the help of https://stackoverflow.com/a/3496655/6318164 answer.

I tried to shorten code best I can to show here.

Net.java

package com.nova.netmanager;

import android.content.Context;
import android.net.wifi.WifiManager;

class Net {
    public boolean isWifiEnabled() {
        WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        return wifi.isWifiEnabled();
    }
}

MainActivity.java

package com.nova.netmanager;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;

public class MainActivity extends AppCompatActivity {
    Switch wifiSwitch;
    WifiManager wifi;

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

        wifiSwitch = (Switch) findViewById(R.id.switch_wifi);
        wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        checkNetworkStatus();

        wifiSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                wifi.setWifiEnabled(isChecked);
            }
        });
    }

    @Override
    protected void onResume() {
         super.onResume();

         checkNetworkStatus();
    }

    public void checkNetworkStatus() {
        Net net = new Net();
        wifiSwitch.setChecked(net.isWifiEnabled());
    }
}

Net.java is my own class I'm trying to make that stores all communication and network stuff together such as checking status and toggling them. Now my app keeps crashing and debugger keeps complaining about this line in Net.java after narrowing it down.

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

I've tried adding the extends AppCompatActivity to class Net but brings up issues saying it should run after onCreate which it does from the script flow logic I see. I've even tried getActivity() that doesn't exist for a class and I've tried using this Context from other answers.

WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

I don't understand why it's so difficult asking the Android if WiFi is on or off in a second script. I thought I've read that Android Studio should be easy even if nobody has coded before.

Any help would be appreciated. Sorry about the long post and possible duplicate.

1

There are 1 best solutions below

1
On BEST ANSWER

You need to pass the context as below

class Net { 
    public boolean isWifiEnabled(Context context) { 
        WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        return wifi.isWifiEnabled();
    } 
}

And to use it in your activity :-

public class MainActivity extends AppCompatActivity { 
    Switch wifiSwitch;
    WifiManager wifi;

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

        wifiSwitch = (Switch) findViewById(R.id.switch_wifi);
        wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        checkNetworkStatus(); 

        wifiSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override 
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                wifi.setWifiEnabled(isChecked);
            } 
        }); 
    } 

    @Override 
    protected void onResume() { 
         super.onResume(); 

         checkNetworkStatus(); 
    } 

    public void checkNetworkStatus() { 
        Net net = new Net();
        wifiSwitch.setChecked(net.isWifiEnabled(MainActivity.this));
    } 
}

Edit : You also need to add following permission in AndroidManifest file

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />