date: June 17, 2015 12:12 AM
categories: android
tags: android
本文介绍了如何检测手机信号质量
MainActivity
package com.example.xuyushi.phonestatelistentest;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TelephonyManager Tel;
TextView tv;
TextView tv2;
MyPhoneStateListener MyListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.text_view);
tv2 = (TextView) findViewById(R.id.text_view2);
MyListener = new MyPhoneStateListener();
Tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Tel.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onResume() {
super.onResume();
Tel.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
private class MyPhoneStateListener extends PhoneStateListener
{
/*
* Get the Signal strength from the provider, each tiome there is an
* update 从得到的信号强度,每个tiome供应商有更新
*/
//这个方法只有在信号强度改变时才调用,或者程序刚刚启动时调用,如果想看到Toast的信号强度提示,那就等信号改变或者重启程序
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
Toast.makeText(getApplicationContext(),
"Go to Firstdroid!!! GSM Cinr = "
+ String.valueOf(signalStrength.getGsmSignalStrength()),
Toast.LENGTH_SHORT).show();
//signalStrength.getGsmSignalStrength()获得的是asu值
tv.setText("asu信号强度"+signalStrength.getGsmSignalStrength() + "");
//dBm =-113+2*asu
int dbm = -113+signalStrength.getGsmSignalStrength()*2;
tv2.setText("dBm信号强度"+dbm + "");
}
}
;
}
xml文件
<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:background="#00ff00"
android:text="@string/hello_world" />
<TextView
android:id="@+id/text_view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:background="#00ffff" />
</LinearLayout>
AndroidMainfest.xml
增加权限
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>