Java Question Passing Context to a Library

Scooter Willis

Member
Licensed User
Longtime User
New to basic4ppc and trying to help my son add a feature he needs for a project.

Need to enable Wifi Hotspot mode. Found some simple code that dynamically loads the hidden class needed to control hotspot on/off.

I have the code in a library but the library needs context and couldn't figure out how to instantiate the class with Context. I added in an initialize method.

B4X:
Dim Wifi As WifiApManager

In ActivityCreate

If FirstTime Then
        Wifi.initialize(Me)
    End If

Get the following error

Wifi.initialize(Me)
javac 1.7.0_13
src\b4a\example\main.java:366: error: inconvertible types
mostCurrent._wifi.initialize((android.content.Context)(main.getObject()));
^
required: Context
found: Class<CAP#1>
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ?


B4X:
public class WifiApManager {
    private  WifiManager mWifiManager;

    public WifiApManager(){
   
    }
   
    public WifiApManager(Context context) {
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    }
   
    public void initialize(Context context){
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    }
}
 

XverhelstX

Well-Known Member
Licensed User
Longtime User
New to basic4ppc and trying to help my son add a feature he needs for a project.

Need to enable Wifi Hotspot mode. Found some simple code that dynamically loads the hidden class needed to control hotspot on/off.

I have the code in a library but the library needs context and couldn't figure out how to instantiate the class with Context. I added in an initialize method.

B4X:
Dim Wifi As WifiApManager

In ActivityCreate

If FirstTime Then
        Wifi.initialize(Me)
    End If

Get the following error

Wifi.initialize(Me)
javac 1.7.0_13
src\b4a\example\main.java:366: error: inconvertible types
mostCurrent._wifi.initialize((android.content.Context)(main.getObject()));
^
required: Context
found: Class<CAP#1>
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ?


B4X:
public class WifiApManager {
    private  WifiManager mWifiManager;

    public WifiApManager(){
  
    }
  
    public WifiApManager(Context context) {
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    }
  
    public void initialize(Context context){
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    }
}

Hi

You need to pass a BA instance in the Initialize method:

B4X:
public class WifiApManager {private WifiManager mWifiManager;
public WifiApManager(){
 
 }
 
 public void initialize(final BA ba){
 mWifiManager = (WifiManager) ba.applicationContext.getSystemService(ba.applicationContext.WIFI_SERVICE);
 }
}

Kind regards,
Tomas
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Dear advanced users,

I little question: I try to compile a library in eclipse to use in B4A, in one method I have a function, its need a CONTEXT, I think is the same like passing "me" from B4A, look:


private Card card;
public String AddCard(Context context)
{
card = new Card(context);
card.setText("Este e apenas um teste de livecard...");
View cardView = card.toView();
setContentView(cardView);
return "";
}


When I pass a "me" from B4A, I can use this way?

Tks in advance
 
Top