0

I have the following classes which are subclasses of AppCompatActivity and PhoneStateListener respectively; I want to take 2 values from the second and print them along with the values from the first one.

MainActivity.java

public class MainActivity extends AppCompatActivity {
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView)findViewById(R.id.info);
    }
    public void onSubmit(View v) {
        TelephonyManager tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        tm.listen(new SecondActivity(this),PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
        String opname="\nOperator ID:"+" "+tm.getNetworkOperator();
        opname=opname+"\nOperator Name:"+" "+tm.getNetworkOperatorName();

        int phoneType=tm.getPhoneType();
        String ptype="";
        switch(phoneType)
        {
            case TelephonyManager.PHONE_TYPE_CDMA:
                ptype="\nPhone Type: CDMA\n";
                break;
            case TelephonyManager.PHONE_TYPE_GSM:
                ptype="\nPhone Type: GSM\n";
                break;
            case TelephonyManager.PHONE_TYPE_SIP:
                ptype="\nPhone Type: SIP\n";
                break;
            case TelephonyManager.PHONE_TYPE_NONE:
                ptype="\nPhone Type: NONE\n";
                break;
        }
        tv.setText(opname+ptype);
    }
}

SecondActivity.java

public class SecondActivity extends PhoneStateListener {
    Context mcontext;
    double value1=0,value2=0;
    String error,ss;
    public SecondActivity(Context context){
        mcontext=context;
    }
    public void onSignalStrengthsChanged(SignalStrength signalStrength){
        super.onSignalStrengthsChanged(signalStrength);
        if (signalStrength.isGsm()) {
            value1=signalStrength.getGsmBitErrorRate();
            error="\nGsmBitErrorRate:"+value1;
            value2=signalStrength.getGsmSignalStrength();
            ss="\nGsmSignalStrength:"+value2;
        } 
    }
}
kamyar haqqani
  • 748
  • 6
  • 19
John Mar
  • 11
  • 6
  • Possible duplicate of [How do I pass data between Activities in Android application?](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – Grimthorr Oct 12 '18 at 11:53
  • You can use Intent, if you want to pass data when going to Activity B from A, you can pass intent extra. eg: Intent i = new Intent(CurrentActivity.this, NewActivity.class); i.putExtra("key",value); startActivity(i); – Zayid Mohammed Oct 12 '18 at 12:07
  • SecondActivity is not actually an activity, it's a class which extends PhoneStateListener. value1 and value2 are dynamically changed as the signal strength changes over time. – kamyar haqqani Oct 12 '18 at 12:40

2 Answers2

0

add a public method to MainActivity which can be accessed from your second class:

public class MainActivity extends AppCompatActivity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv=(TextView)findViewById(R.id.info);
}
public void onSubmit(View v) {
    TelephonyManager tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    tm.listen(new SecondActivity(this),PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
    String opname="\nOperator ID:"+" "+tm.getNetworkOperator();
    opname=opname+"\nOperator Name:"+" "+tm.getNetworkOperatorName();

    int phoneType=tm.getPhoneType();
    String ptype="";
    switch(phoneType)
    {
        case TelephonyManager.PHONE_TYPE_CDMA:
            ptype="\nPhone Type: CDMA\n";
            break;
        case TelephonyManager.PHONE_TYPE_GSM:
            ptype="\nPhone Type: GSM\n";
            break;
        case TelephonyManager.PHONE_TYPE_SIP:
            ptype="\nPhone Type: SIP\n";
            break;
        case TelephonyManager.PHONE_TYPE_NONE:
            ptype="\nPhone Type: NONE\n";
            break;
    }
    tv.setText(opname+ptype);
}

//declare a public method which is accessable in onSignalStrengthsChanged callback in the second class.
        public void do_what_you_wanna_do_with_these_two_values(double value1,double value2){
            tv.setText("here are my values: "+String.valueOf(value1)+" , "+String.valueOf(value1));
        }
}

and use it to pass info to MainActivity:

public class SecondActivity extends PhoneStateListener {
    Context mcontext;
    double value1=0,value2=0;
    String error,ss;
    private MainActivity first_activity_instance;
    public SecondActivity(Context context){
        mcontext=context;
        first_activity_instance =(MainActivity)mcontext;
    }
    public void onSignalStrengthsChanged(SignalStrength signalStrength){
        super.onSignalStrengthsChanged(signalStrength);
        if (signalStrength.isGsm()) {
            value1=signalStrength.getGsmBitErrorRate();
            error="\nGsmBitErrorRate:"+value1;
            value2=signalStrength.getGsmSignalStrength();
            ss="\nGsmSignalStrength:"+value2;
            //now pass them to your to your activity instance
            first_activity_instance.do_what_you_wanna_do_with_these_two_values(value1,value2);
        }
    }
}
kamyar haqqani
  • 748
  • 6
  • 19
  • Thank you for your answer ,but SecondActivity can't find the method from the MainActivity.Does it need anything else into code? – John Mar Oct 13 '18 at 11:43
  • that's impossible. the method is a public property of MainActivity so the instance you pass to secondActivity( new SecondActivity(this)) must have the method. maybe you didn't refresh your project or something. – kamyar haqqani Oct 13 '18 at 14:25
  • Thank you ,I have done a mistake and there was the problem – John Mar Oct 14 '18 at 14:34
-1

Actually create and register broadcast receiver for signal strength and then pass the data to activity with intent...

LifeStyle
  • 411
  • 2
  • 10