forum.php.jpg  

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 */
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()