Android中获取信号强度

 

 

2011-3-27 10:30|发布者: Vincent|查看: 5009|评论: 7

摘要: How to get the Quality of the Signal received by our phone. This tutorial we’ll teach you how to get the signal strength you receive at any moment from your Carrier provider. Lets start with the Tuto ...
How to get the Quality of the Signal received by our phone. This tutorial we’ll teach you how to get the signal strength you receive at any moment from your Carrier provider. Lets start with the Tutorial: We are going to learn how to add a listener to the telephony class, and how to get the CINR (Signal Quality) from this listener. We need to add permissions for the Activity Add the fallowing permission: android.permission.CHANGE_NETWORK_STATE The “AndroidManifest.xml” file should looks as below:
  1. < ?xml version="1.0" encoding="utf-8"?>
    < ?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="Firstdroid.Tutorial.GetGsmSignalStrength"
    android:versionCode="1"
    android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".GetGsmSignalStrength"
    android:label="@string/app_name">
    <intent -filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent>
    </activity>
    
    </application>
    <uses -sdk android:minSdkVersion="4" />
    <uses -permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses>
    </manifest>


复制代码
Now lets start with the code. All the explanation are already built in inside the code, please read the remarks. Add the imports we will need
  1. import android.app.Activity;
  2. import android.content.Context;
  3. import android.os.Bundle;
  4. import android.telephony.PhoneStateListener;
  5. import android.telephony.SignalStrength;
  6. import android.telephony.TelephonyManager;
  7. import android.widget.Toast;
复制代码
Now the class with the fallowing methods:
onResume, Called when application restarts after being minimized
onPause, Called when the application is being minimized
onCreate, Called when the application is first started
private class MyPhoneStateListener, Called to create the listener
The code goes as fallows:
  1. public class GetGsmSignalStrength extends Activity
    {
    /* This variables need to be global, so we can used them onResume and onPause method to
    stop the listener */
    TelephonyManager Tel;
    MyPhoneStateListener MyListener;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    /* Update the listener, and start it */
    MyListener = new MyPhoneStateListener();
    Tel = ( TelephonyManager )getSystemService(Context.TELEPHONY_SERVICE);
    Tel.listen(MyListener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
    }
    
    /* Called when the application is minimized */
    @Override
    protected void onPause()
    {
    super.onPause();
    Tel.listen(MyListener, PhoneStateListener.LISTEN_NONE);
    }
    
    /* Called when the application resumes */
    @Override
    protected void onResume()
    {
    super.onResume();
    Tel.listen(MyListener,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
    }
    
    /* —————————– */
    /* Start the PhoneState listener */
    /* —————————– */
    private class MyPhoneStateListener extends PhoneStateListener
    {
    /* Get the Signal strength from the provider, each tiome there is an update */
    @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();
    }
    
    };/* End of private Class */
    
    }/* GetGsmSignalStrength */
    


复制代码
That is all. You should now see the fallowing:
snap20100512_161029-180x300.png
2011-3-27 10:18:35 上传
下载附件(11.86 KB)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值