Reliable way to tell if running on phone or tablet for Android?

Hi, is there anything conclusive that can be tested for this info? thx

There’s no good, explicit, way… You could check the resolution, or use android.telephony.TelephonyManager.getPhoneType() to know if there’s a phone service installed.

thx - figured as much, but just thought I’d check!

Hi, sorry for reviving this old thread, but I can’t seem to find any more info on this.

What do I have to include to access the TelephonyManager class in Juce?

I found out that I can get the instance of the TelephonyManager like this:
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

but juce doesn’t know TelephonyManager. So i guess I’ll have to include some files, but I can’t figure out which ones…

That is Java code, so you need to do it in e.g. JuceApp.java or JuceActivity.java. You could also call it via JNI from your C++ code.

Some example code for this sort of thing: https://gist.github.com/Jiezhi/79c04de92762ed5ac890

1 Like

Ah that explains a lot. Thanks for the help and also thanks for the example code, this is great help! Can’t wait to give this a shot tomorrow

I’ve been banging my head against this for a week and just can’t figure it out. There are so many questions, that I can’t seem to find an answer for.

I know I need a .java file that contains the java function, I know there are some variables already present somewhere (environment, context,…) that I need, I know that I have to declare my functions through JNI.

I’ve looked through JUCE’s own implementations (e.g. JNI_Helpers), I tried to gather info from the link up there on how this works (but it doesn’t really tell me anything about the relation between what’s written in there and my C++/.java files, does it?) and at every turn I take I just run into problems that I don’t even understand.

@ed95 from Github I gather, that you worked on the JNI Helpers. Would you (or anyone else) mind giving me a simple example on how to implement a java function into a c++ file?

Say something very basic like this:

int add(int a, int b)
{
  return a + b;
}
  • How do I correctly write this in a .java file (like what’s the right format to do this to use it with JNI afterwards)?
  • Do I just include that .java file into my .h file?
  • How do I then declare/call this function from my .h file?

Sidenote: Wouldn’t it make sense to include the info if it’s a phone running or not in the juce::SystemStats class? To differentiate on iOS I just call SystemStats::getDeviceDescription() and ask if the string returned contains iPhone… simple as that. For Android this approach obviously doesn’t work. It seems crucial to me to have that info about the device available at runtime, when writing Juce apps that are supposed to go to Android/iOS on both tablets and phones…

See this project for a comprehensive example of using JNI to communicate between C++ and Java in a JUCE app: https://github.com/adamski/audioengine-demo/blob/master/DemoAudioEngine/Source/AndroidBindings.cpp

1 Like

After weeks of on/off banging my head against this I’ve figured it out and here’s how to get the TelephonyManager.getPhoneType() result in Juce for anyone else who needs it.

Also I think this is generally a very simple example on how one can call native android functions from Juce, so I thought it’s worth sharing

Please correct me if anything is horribly wrong here, but this seems to work for me

#if JUCE_ANDROID
#include <juce_core/native/juce_android_JNIHelpers.h>

namespace DeviceTypeDetector
{

#define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  METHOD (getPhoneType, "getPhoneType", "()I") \

DECLARE_JNI_CLASS (TelephonyManager, "android/telephony/TelephonyManager")
#undef JNI_CLASS_MEMBERS

static int getPhoneTypeFromTelephonyManager()
{
    auto* env = getEnv();
   LocalRef<jobject> telephonyManager (env->CallObjectMethod(getAppContext().get(),
                                                                     AndroidContext.getSystemService,
                                                                     javaString ("phone").get()  ));

    int result =  -1;
    if (telephonyManager != nullptr)
    {
        result = env->CallIntMethod (telephonyManager.get(), TelephonyManager.getPhoneType);
    }
    DBG("Phone result: " << result);
    return result;

}
} //end namespace DeviceTypeDetector
#endif
1 Like