如何将数据从PhoneStateListener子类发送到活动?

时间:2018-10-12 11:53:02

标签: java android android-activity

我有以下类别,分别是AppCompatActivityPhoneStateListener的子类别;我想从第二个中获取2个值,并将它们与第一个中的值一起打印。

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;
        } 
    }
}

2 个答案:

答案 0 :(得分:0)

向MainActivity添加一个公共方法,可以从第二个类访问该方法:

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));
        }
}

并将其用于将信息传递给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);
        }
    }
}

答案 1 :(得分:-1)

实际上创建并注册广播接收器以获得信号强度,然后有意地将数据传递给活动...