Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 中級開發 >> Android中獲取信號強度

Android中獲取信號強度

編輯:中級開發

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"?>
     
  2. < ?XML version="1.0" encoding="utf-8"?>
     
  3. <manifest XMLns:android="http://schemas.android.com/apk/res/android"
     
  4.       package="Firstdroid.Tutorial.GetGsmSignalStrength"
     
  5.       android:versionCode="1"
     
  6.       android:versionName="1.0">
     
  7.     <application android:icon="@drawable/icon" android:label="@string/app_name">
     
  8.         <activity android:name=".GetGsmSignalStrength"
     
  9.                   android:label="@string/app_name">
     
  10.             <intent -filter>
     
  11.                 <action android:name="android.intent.action.MAIN" />
     
  12.                 <category android:name="android.intent.category.LAUNCHER" />
     
  13.             </intent>
     
  14.         </activity>
     

  15.  
  16.     </application>
     
  17.     <uses -sdk android:minSdkVersion="4" />
     
  18.     <uses -permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses>
     
  19. </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
     
  2. {
     
  3.    /* This variables need to be global, so we can used them onResume and onPause method to
     
  4.       stop the listener */
     
  5.    TelephonyManager        Tel;
     
  6.    MyPhoneStateListener    MyListener;
     

  7.  
  8.    /** Called when the activity is first created. */
     
  9.     @Override
     
  10.     public void onCreate(Bundle savedInstanceState)
     
  11.     {
     
  12.         super.onCreate(savedInstanceState);
     
  13.         setContentVIEw(R.layout.main);
     

  14.  
  15.         /* Update the listener, and start it */
     
  16.         MyListener   = new MyPhoneStateListener();
     
  17.         Tel       = ( TelephonyManager )getSystemService(Context.TELEPHONY_SERVICE);
     
  18.       Tel.listen(MyListener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
     
  19.     }
     

  20.  
  21.     /* Called when the application is minimized */
     
  22.     @Override
     
  23.    protected void onPause()
     
  24.     {
     
  25.       super.onPause();
     
  26.       Tel.listen(MyListener, PhoneStateListener.LISTEN_NONE);
     
  27.    }
     

  28.  
  29.     /* Called when the application resumes */
     
  30.    @Override
     
  31.    protected void onResume()
     
  32.    {
     
  33.       super.onResume();
     
  34.       Tel.listen(MyListener,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
     
  35.    }
     

  36.  
  37.    /* —————————– */
     
  38.     /* Start the PhoneState listener */
     
  39.    /* —————————– */
     
  40.     private class MyPhoneStateListener extends PhoneStateListener
     
  41.     {
     
  42.       /* Get the Signal strength from the provider, each tiome there is an update */
     
  43.       @Override
     
  44.       public void onSignalStrengthsChanged(SignalStrength signalStrength)
     
  45.       {
     
  46.          super.onSignalStrengthsChanged(signalStrength);
     
  47.          Toast.makeText(getApplicationContext(), "Go to Firstdroid!!! GSM Cinr = "
     
  48.             + String.valueOf(signalStrength.getGsmSignalStrength()), Toast.LENGTH_SHORT).show();
     
  49.       }
     

  50.  
  51.     };/* End of private Class */
     

  52.  
  53. }/* GetGsmSignalStrength */
     

  54.  
復制代碼

That is all. You should now see the fallowing:

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved