Android Question Using Java code for the first time

AHilberink

Active Member
Licensed User
Longtime User
Hi,

I have problems by using Java code for the first time. I don't know if it is my lack of knowledge or just not the right code.

I try:
B4X:
Sub GetCountry As String
    Private NativeMe As JavaObject
   
    If(NativeMe.IsInitialized=False) Then NativeMe.InitializeContext
   
    Dim s As String = NativeMe.RunMethod("getUserCountry", Null)
    Log(s)
    Return s
   
#if Java
/**
* Get ISO 3166-1 alpha-2 country code for this device (or null if not available)
* @param context Context reference to get the TelephonyManager instance from
* @return country code or null
*/
public static String getUserCountry(Context context) {
    try {
        final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
           final String simCountry = tm.getSimCountryIso();
           if (simCountry != null && simCountry.length() == 2) { // SIM country code is available
               return simCountry.toLowerCase(Locale.US);
           }
           else if (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA) { // device is not 3G (would be unreliable)
            String networkCountry = tm.getNetworkCountryIso();
               if (networkCountry != null && networkCountry.length() == 2) { // network country code is available
                   return networkCountry.toLowerCase(Locale.US);
               }
           }
       }
       catch (Exception e) { }
       return null;
}
#End If
End Sub

This is the error during compile:
Compileren gegenereerde Java code. Error
src\ciris\chauffeur\main.java:1781: error: cannot find symbol
final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
^
symbol: class TelephonyManager
location: class main
1 error

javac 11.0.1


Can somebody help me with this?

BR, André
 

stevel05

Expert
Licensed User
Longtime User
You need to import the required classes for the java code, and you need to pass the context to the called sub. Try this:

B4X:
Sub GetCountry As String
    Private NativeMe As JavaObject
   
    NativeMe = NativeMe.InitializeContext
   
    Dim s As String = NativeMe.RunMethod("getUserCountry", Array(NativeMe))
    Log(s)
    Return s
End Sub
   
#if Java

import android.content.Context;
import android.telephony.TelephonyManager;
import java.util.Locale;

/**
* Get ISO 3166-1 alpha-2 country code for this device (or null if not available)
* @param context Context reference to get the TelephonyManager instance from
* @return country code or null
*/



public static String getUserCountry(Context context) {
    try {
        final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
           final String simCountry = tm.getSimCountryIso();
           if (simCountry != null && simCountry.length() == 2) { // SIM country code is available
               return simCountry.toLowerCase(Locale.US);
           }
           else if (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA) { // device is not 3G (would be unreliable)
            String networkCountry = tm.getNetworkCountryIso();
               if (networkCountry != null && networkCountry.length() == 2) { // network country code is available
                   return networkCountry.toLowerCase(Locale.US);
               }
           }
       }
       catch (Exception e) { }
       return null;
}
#End If
 
Upvote 0

AHilberink

Active Member
Licensed User
Longtime User
You need to import the required classes for the java code, and you need to pass the context to the called sub. Try this:

B4X:
Sub GetCountry As String
    Private NativeMe As JavaObject
  
    NativeMe = NativeMe.InitializeContext
  
    Dim s As String = NativeMe.RunMethod("getUserCountry", Array(NativeMe))
    Log(s)
    Return s
End Sub
  
#if Java

import android.content.Context;
import android.telephony.TelephonyManager;
import java.util.Locale;

/**
* Get ISO 3166-1 alpha-2 country code for this device (or null if not available)
* @param context Context reference to get the TelephonyManager instance from
* @return country code or null
*/



public static String getUserCountry(Context context) {
    try {
        final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
           final String simCountry = tm.getSimCountryIso();
           if (simCountry != null && simCountry.length() == 2) { // SIM country code is available
               return simCountry.toLowerCase(Locale.US);
           }
           else if (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA) { // device is not 3G (would be unreliable)
            String networkCountry = tm.getNetworkCountryIso();
               if (networkCountry != null && networkCountry.length() == 2) { // network country code is available
                   return networkCountry.toLowerCase(Locale.US);
               }
           }
       }
       catch (Exception e) { }
       return null;
}
#End If

Thanks, Steve.

This is what I looked for.

BR, André
 
Upvote 0
Top