检测手机信号质量

检测手机信号质量

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"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值