获取手机信号强度

要想获取信号强度,需要加入权限:

<uses -permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses>

AndroidManifest.xml

< ?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>


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.widget.Toast;
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  从得到的信号强度,每个tiome供应商有更新*/
 
      @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 */





  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值