logo资料库

Android中获取GSM、CDMA、Wifi信号强度.docx

第1页 / 共20页
第2页 / 共20页
第3页 / 共20页
第4页 / 共20页
第5页 / 共20页
第6页 / 共20页
第7页 / 共20页
第8页 / 共20页
资料共20页,剩余部分请下载后查看
Android中获取信号强度
Android中获取信号强度
android 获取手机GSM/CDMA信号信息
Android 获取信号强度 如何获得信号的质量受到我们的电话。这篇教程我们将教你如何接收信号强度你随时提 供从你的载体。让我们开始以教程:我们将继续学习如何添加一个监听器到电话类,以及 如何获得 CINR 信号质量)(我们需要添加权限允许添加接下来的活 动:android.permission.CHANGE_NETWORK_STATE“AndroidManifest.xml”文件应该看 起来如下: java 代码: 1. < ?xml version="1.0" encoding="utf-8"?> 2. 8. 9. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 现在让我们开始你的代码。所有的解释就是已建在代码,请阅读这些言论。我们需要增加 进口 java 代码: 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; onResume,称为申请时重新启动后,被最小化 onPause,称为当应用程序被最小化 onCreate 当应用程序,被称为是开始 MyPhoneStateListener 私人课,称为创造听众 java 代码: 1. public class GetGsmSignalStrength extends Activity{ 2. 3. TelephonyManager Tel; 4. MyPhoneStateListener MyListener; 5. /** Called when the activity is first created. */ 6. 7. @Override 8. public void onCreate(Bundle savedInstanceState){ 9. 10. super.onCreate(savedInstanceState); 11. setContentView(R.layout.main); 12. 13. MyListener = new MyPhoneStateListener(); 14. Tel = ( TelephonyManager )getSystemService(Context.TELEPHONY_SERVICE); 15. Tel.listen(MyListener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTH S); 16. } 17. 18. 19. @Override 20. protected void onPause(){ 21. super.onPause(); 22. Tel.listen(MyListener, PhoneStateListener.LISTEN_NONE); 23. } 24. 25. @Override 26. protected void onResume(){ 27. super.onResume(); 28. Tel.listen(MyListener,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS ); 29. }
30. 31. private class MyPhoneStateListener extends PhoneStateListener{ 32. 33. @Override 34. public void onSignalStrengthsChanged(SignalStrength signalStrength){ 35. super.onSignalStrengthsChanged(signalStrength); 36. Toast.makeText(getApplicationContext(), "Go to Firstdroid!!! GSM Cinr = "+ String.valueOf(signalStrength.getGsmSignalStrength()), Toast.LENGTH_SHORT).show(); 37. } 38. 39. }; 40. 41. 42. } Android 中获取信号强度 时间:2011 年 03 月 31 日 10:22:38 来源:DevDiv 作者:Vincent 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.
5. 6. 7. 8. 9. 10. 11. 12. 13. android:versionCode="1" android:versionName="1.0"> 14. 15. 16. 17. 18. 19. 复制代码
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. 2. 3. 4. 5. 6. 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;
7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. /** 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 */ 26. @Override 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 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 */
38. 39. 40. 41. 42. 43. 44. 45. @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 */ 46. }/* GetGsmSignalStrength */ Android 中获取信号强度 2011-3-27 10:30| 发布者: Vincent| 查看: 5826| 评论: 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: package="Firstdroid.Tutorial.GetGsmSignalStrength" android:versionCode="1" android:versionName="1.0"> 1. < ?xml version="1.0" encoding="utf-8"?> 2. < ?xml version="1.0" encoding="utf-8"?> 3.
13. 14. 15. 16. 17. 18. 19. 复制代码 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. 2. 3. 4. 5. 6. 7. 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; 复制代码 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 stop the listener */ TelephonyManager MyPhoneStateListener MyListener; Tel; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.
分享到:
收藏