How to Communicate a Fragment with PhoneStateListener

124 Views Asked by At

I have the following 2 classes the first it's a tab for Tabbed activity and the second it used to take LTE connection data from the phone. I need to print the values into the fragment. Before this, the code of first class was into an Activity but needed to move into extends Fragment.

public class Tab1Values extends Fragment {
    TextView tv1,tv2,tv3;
    int value=0,dbm=0;
    String ptype="",ntype="";
    public Tab1Values() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.fragment_tab1, container, false);
        tv1=(TextView)v.findViewById(R.id.info);
        tv2=(TextView)v.findViewById(R.id.info2);
        tv3=(TextView)v.findViewById(R.id.info3);
        TelephonyManager tm=(TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);
        ->tm.listen(new Tab1Signal2(this.getActivity()), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
        String opname="\nOperator ID:"+" "+tm.getNetworkOperator();
        opname=opname+"\nOperator Name:"+" "+tm.getNetworkOperatorName();
        int phoneType=tm.getPhoneType(),networkType=tm.getNetworkType();

        return v;
    }
}

SecondClass

public class Tab1Signal2 extends PhoneStateListener {
    Context mcontext;
    int LteSignalStrength=0,LteRsrp=0,LteRsrq=0,LteRssnr=0,LteCqi=0;
    int value1=0,value2=0,CdmaDbm=0,CdmaEcio=0,EvdoDbm=0,EvdoEcio=0,EvdoSnr=0;
    //int cid=0,lac=0,psc=0,statid=0,netid=0,sysid=0,lat=0,lon=0;
    String error,ss,val1,ccloc;
    private Tab1Values main;

    public Tab1Signal2(Context context){
        mcontext=context;
        ->main=(Tab1Values) mcontext;
    }
    public void onSignalStrengthsChanged(SignalStrength signalStrength){
        super.onSignalStrengthsChanged(signalStrength);
        if (signalStrength.isGsm()) {
            value1=signalStrength.getGsmBitErrorRate();
            value2=signalStrength.getGsmSignalStrength();
        }else if (signalStrength.getCdmaDbm() > 0) {
            CdmaDbm=signalStrength.getCdmaDbm();
            CdmaEcio=signalStrength.getCdmaEcio();
        } else {
            EvdoDbm=signalStrength.getEvdoDbm();
            EvdoEcio=signalStrength.getEvdoEcio();
            EvdoSnr=signalStrength.getEvdoSnr();
        }
        try {
            Method[] methods = android.telephony.SignalStrength.class
                    .getMethods();
            for (Method mthd : methods) {
                if (mthd.getName().equals("getLteSignalStrength")){
                    //val1=mthd.getName() ;
                    LteSignalStrength=(Integer)mthd.invoke(signalStrength);
                    //main.test(val2);
                }
                if (mthd.getName().equals("getLteRsrp")){
                    LteRsrp=(Integer)mthd.invoke(signalStrength);
                }
                if (mthd.getName().equals("getLteRsrq")){
                    LteRsrq=(Integer)mthd.invoke(signalStrength);
                }
                if (mthd.getName().equals("getLteRssnr")){
                    LteRssnr=(Integer)mthd.invoke(signalStrength);
                }
                if (mthd.getName().equals("getLteCqi")){
                    LteCqi=(Integer)mthd.invoke(signalStrength);
                }
                main.test(LteSignalStrength,LteRsrp,LteRsrq,LteRssnr,LteCqi);
            }
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

I have put an arrow at the lines that I think it is the mistake. Thank you

1

There are 1 best solutions below

0
On

You can use EventBus to pass the needed data

https://github.com/greenrobot/EventBus

Event to be sent, you can add fields that you want to pass:

public static class MessageEvent { /* Additional fields if needed */ }

Add function for eventBus listening:

@Subscribe(threadMode = ThreadMode.MAIN)  
public void onMessageEvent(MessageEvent event) {/* Do something */};

Register event in fragment (and unregister) for example:

 @Override
 public void onStart() {
     super.onStart();
     EventBus.getDefault().register(this);
 }

 @Override
 public void onStop() {
     super.onStop();
     EventBus.getDefault().unregister(this);
 }

And post event:

EventBus.getDefault().post(new MessageEvent());