Android Question java in class module

Stern0m1

Member
Licensed User
Im having an issue with jave code in a class module, which works fine in an activity module.

B4A version: 6.50
Parsing code. (0.03s)
Compiling code. (0.16s)
Compiling layouts code. (0.02s)
Organizing libraries. (0.00s)
Generating R file. (0.06s)
Compiling generated Java code. Error
B4A line: 49
End Sub
javac 1.8.0_111
src\b4a\RELEASE\startcar.java:361: error: cannot find symbol
WifiManager mw = (WifiManager) getSystemService(Context.WIFI_SERVICE);
^
symbol: method getSystemService(String)
location: class startcar

here's the code:
B4X:
#If JAVA
import android.net.wifi.*;
import android.content.Context;
import java.util.List;
import java.lang.Object.*;
import android.widget.Toast;

public boolean ison ()
{

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

return mw.isWifiEnabled();

}

Thanks
 

DonManfred

Expert
Licensed User
Longtime User
The Wifimanager needs an context i guess...

So i think it will only work in an activity or an service as these runs the inline java in it´s own context.

Maybe you need to give the Activities context to the class (when initialing for ex) and then use this context. But then i think it would be better to place the java directly in the activity.

I didn´t do much with inline java so i cant´t help more on this.
 
Last edited:
Upvote 0

Stern0m1

Member
Licensed User
After 5 hours i figure it out by adding
B4X:
Context context = BA.applicationContext;


WifiManager mw = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
It works.

I tried using mlwifi library buts its buggy and didnt work, that prompted me to check his source code. From there I figured it out.
 
Upvote 0
Top