-2

im making and APP about the Network information etc. I got a problem when i want to use TelephonyManager in different class then my MainActivity class. When i have a simple code in one class then everything is working OK, but the problem is when i want to make another class. Program then is compiling without any problem, but the APP is crashing.

Working code in one class:

MainActivity.java

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final TextView textMCC = findViewById(R.id.mcc);
    textMCC.setText(getMCC());
}
public String getMCC() {
    TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    String networkOperator = telephonyManager.getNetworkOperator();
    return networkOperator.substring(0, 3);
}

When i try to split this to two different classes, MainActivity.java and Cell.java the APP is crashing.

Main Activity:

public class MainActivity extends AppCompatActivity {


 Cell cellex = new Cell();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final TextView textMCC = findViewById(R.id.mcc);
    textMCC.setText(cellex.getMCC());
}
}

Cell.java:

public class Cell {
Context context;
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

public String getMCC() {
    // TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    String networkOperator = telephonyManager.getNetworkOperator();
    return networkOperator.substring(0, 3);
}
}

Before adding line: Context context, the program didn't compile.

Headsman
  • 25
  • 5
  • You just added a variable, never initialized it, and tried to call a function on it. You may want to read about how variables work in Java - it's different than, say, C++. – Ryan M Mar 31 '20 at 09:54
  • I know, but when i want to start it without Context context, program is not compiling. Do you know maybe how to implement TelephonyManager object in other class then main class? Or maybe i need to make object in main class and then pass it through method? – Headsman Mar 31 '20 at 12:13

1 Answers1

1

This happens because Activity has method getSystemService which forwards the request to Context.getSystemService(). So you have to send param Context inside getMCC().

String getMCC(Context content){ TelephonyManager telephonyManager = context.getSystemService(Context.TELEPHONY_MANAGER_SERVICE); }

String mcc = cell.getMCC(MainActivity.this);

  • When i try to put this inside the method ` public String getMCC() { TelephonyManager telephonyManager = (TelephonyManager) Context.getSystemService(Context.TELEPHONY_SERVICE); //rest of the method code }` It gives me an error: error: non-static method getSystemService(String) cannot be referenced from a static context TelephonyManager telephonyManager = (TelephonyManager) Context.getSystemService(Context.TELEPHONY_SERVICE); – Headsman Mar 31 '20 at 14:08
  • 1
    my mistake. getSystemService() is not a static method.. Please use my updated answer – Abhilash Bhaduri Mar 31 '20 at 14:17
  • Thanks mate! Apps stop crushing. I actually tried something similar, but my syntax must be wrong. – Headsman Mar 31 '20 at 14:32