如何与PhoneStateListener交流片段

时间:2018-11-09 14:23:40

标签: java android phone-state-listener

我有以下2个类别,第一个类别是Tabbed活动的标签,第二个类别用于从电话中获取LTE连接数据。我需要将值打印到片段中。在此之前,一流的代码进入了Activity,但需要移至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();
        }
    }
}

我在箭头上划了一条线,我认为这是错误的。 谢谢

1 个答案:

答案 0 :(得分:0)

您可以使用EventBus传递所需的数据

https://github.com/greenrobot/EventBus

要发送的事件,您可以添加要传递的字段:

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

添加事件总线监听功能:

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

在片段中注册事件(并取消注册),例如:

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

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

发布事件:

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