Android: Access system APIs (not sensors) with Processing. "Cannot make a static reference to a non static method. "

Hey guys, so im pretty confident with regular processing but this java-Android stuff is killing me. Here is my problem:

I want the result of this API: https://developer.android.com/reference/android/telephony/CellSignalStrengthLte , specifically the getDbm() method.

I read a lot, even bought an eBook, but all examples I found only read sensors. They then include a lot of objects like listeners, managers etc. I tried to find those for android.telephony but no real success.

I know this could be solved by “learning java” but I really only need this one thing. Any help?

The (to me) obvious but wrong import android.content.Context; import android.telephony.TelephonyManager; import android.telephony.CellSignalStrengthLte;

and then: var = CellSignalStrengthLte.getDbm(); fails with cannot make a static reference to a non static method.

Any help would be fantastic!

Could you post the code you managed until now?

@Evlmnkey === your error has nothing to see with android; very classical java stuff!

Its totally a Java problem, I know. Unfortuately I only really need this one nugget and would be fine otherwise. Don’t have the time to learn java from the ground up for that. So This:

import android.content.Context;
import android.telephony.TelephonyManager;
import android.telephony.CellSignalStrengthLte;

Context context;
CellSignalStrengthLte foo;


int myResult;

void setup() {
  fullScreen();
  context = getActivity(); 
}

void draw() {
  background(0);
   myResult =  foo.getDbm();
    text(str(myResult), 0, 0, width, height);
}

crashes with a NullPointerException: java.lang.NullPointerException: Attempt to invoke virtual method ‘int android.telephony.CellSignalStrengthLte.getDbm()’ on a null object reference

I am pretty sure I need to initialize “foo” somehow but how? And where would I find out on the page I linked with all the android APIs?

Activity and context are different things

import android.app.Activity;
import android.content.Context;

Activity act;
Context cnxt;

void setup() {
  act = this.getActivity();
 cnxt = act.getApplicationContext();
}

I see, I played around with that a bit but don’t really see how that is relevant to my problem.

I just need to read the output of the getSignalStrengthLte.getDbm() method into a variable. How can I do that in processing?

@Evlmnkey ===

  • The error you put is note the same as the first one…
  • you have to create an instance of TelephonyManager, then see code below (from yours, without trying to modify)
  • dont forget to ask permissions on runtime

import android.Manifest;///for asking permissions run time
import android.telephony.CellInfo;
import java.util.List;
import android.telephony.CellInfoLte;
import android.telephony.CellSignalStrengthLte;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;///for knowing wether your phone is running more than //LollyPop
import android.telephony.TelephonyManager;

Context context;
CellSignalStrengthLte foo;
TelephonyManager telephonyManager;
Activity act;
int myResult;
int MyVersion = Build.VERSION.SDK_INT;

void setup() {
  fullScreen();
  act = this.getActivity();
  context = getActivity().getApplicationContext();
  if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
                //here ask for permissions on runtime (access coarse location && read phone state)
};

   telephonyManager = (TelephonyManager) act.getSystemService(Context.TELEPHONY_SERVICE);//get instance of TelephonyManager
   
   List<CellInfo> cellInfoList = telephonyManager.getAllCellInfo();//create a list for all infos about the phone cells

    for (final CellInfo info : cellInfoList) {
  
               println(info);

                if (info instanceof CellInfoLte) {//as you want to use Lte
                   foo = ((CellInfoLte) info).getCellSignalStrength();
                   println(foo);
                   myResult = foo.getDbm();//the value you are wanting to get
                     
                }
    }

};

void draw() {
  background(0);
   myResult =  foo.getDbm();
    text(str(myResult), 0, 0, width, height);
};

Thanks so much! unfortunately, this whole permission thing keeps biting me.

This is what I did:

import android.Manifest;///for asking permissions run time
import android.telephony.CellInfo;
import java.util.List;
import android.telephony.CellInfoLte;
import android.telephony.CellSignalStrengthLte;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;///for knowing wether your phone is running more than //LollyPop
import android.telephony.TelephonyManager;

Context context;
CellSignalStrengthLte foo;
TelephonyManager telephonyManager;
Activity act;
int myResult;
int MyVersion = Build.VERSION.SDK_INT;

void setup() {
  fullScreen();
  act = this.getActivity();
  context = getActivity().getApplicationContext();
  if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
    requestPermission("android.permission.ACCESS_COARSE_LOCATION", "initLoc");
    requestPermission("android.permission.READ_PHONE_STATE", "initPS");
    requestPermission("android.permission.ACCESS_NETWORK_STATE", "initNW");

                //here ask for permissions on runtime (access coarse location && read phone state)
};

   telephonyManager = (TelephonyManager) act.getSystemService(Context.TELEPHONY_SERVICE);//get instance of TelephonyManager
   
   List<CellInfo> cellInfoList = telephonyManager.getAllCellInfo();//create a list for all infos about the phone cells

    for (final CellInfo info : cellInfoList) {
  
               println(info);

                if (info instanceof CellInfoLte) {//as you want to use Lte
                   foo = ((CellInfoLte) info).getCellSignalStrength();
                   println(foo);
                   myResult = foo.getDbm();//the value you are wanting to get                
                }
    }
};


void draw() {
  background(0);
   myResult =  foo.getDbm();
    println(myResult);
};

void initLoc(boolean granted) {
  if (granted) {   
    println("init location manager");
  } else {
    println("location is not available");
exit();
  }
}
void initPS(boolean granted) {
  if (granted) {   
    println("Phonestate Granted");
  } else {
    println("Phonestate not granted");
exit();
  }
}

void initNW(boolean granted) {
  if (granted) {   
    println("NW Granted");
  } else {
    println("NW not granted");
   exit();
  }
}

I added both permissions you said I would need, (and toyed around with some others like “network state”, but the app keeps crashing on launch with:
“FATAL EXCEPTION: Animation Thread
Process: processing.test.sketch_200511b, PID: 16376
java.lang.SecurityException: Not allowed to access cell info”

Without ever asking for any permission
Is there a syntax/unterstanding-problem? I thought the permission-line would call the specified function, which then just runs through and the code continues. What is wrong with that line of thinking?

Again, thanks so much for your help! I realize this is pretty far on the “LMGTFY”-side of questions but I really tried and this whole Android/Java-Lingo is just massively confusing if you’re not used to it.

@Evlmnkey == ok; that is a way to ask “pernicious permissions” ( not the best but…) - and so, whap happens (about your previous and changed errors)-
I f it works (i am not sure!) put answered for others…

No it does not work. I misformatted my post and you were so quick, sorry. Explanation at the bottom. The app crashes with missing permissions on startup (but never asks).

Any ideas for me to try?

@Evlmnkey=== yes, i have posted here a lot of times “how to get permissions” on runtime—try to look (and dont forget that the first time you are launching the app it will ask you ‘do you accept?’)