MainActivity.java
package com.example.sample_phonesignalstrength; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.telephony.PhoneStateListener; import android.telephony.SignalStrength; import android.telephony.TelephonyManager; import android.widget.TextView; public class MainActivity extends Activity { private TextView textView1; private TelephonyManager mTelephonyManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1 = (TextView) this.findViewById(R.id.textView1); mTelephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); mTelephonyManager.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS); } private class MyPhoneStateListener extends PhoneStateListener{ @Override public void onSignalStrengthsChanged(SignalStrength signalStrength) { super.onSignalStrengthsChanged(signalStrength); if(mTelephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM){ textView1.setText("GSM Strength"+signalStrength.getGsmSignalStrength()); } else if(mTelephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA){ textView1.setText("CDMA Strength"+signalStrength.getCdmaDbm()+" dBm"); } else{ textView1.setText("Unknown PhoneType: "+mTelephonyManager.getPhoneType()); } } } }
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="TextView" android:textSize="30sp" /> </RelativeLayout>
沒有留言 :
張貼留言