大 家看到标题就明白了,我们有的时候在电梯里,有时有的电话有信号,有时有的电话没有信号,这个是非常闹心的,要是我们能监听一下我们自己手机信号的强度就 好了。这样,当我们在等重要的人士的电话,不至于接不到这个电话,如果是这个电话你没有接到的话,可能你的一生就这么平淡的过去,如果你接到了这个电话的 话,也许你的一生就会发生改变。我们应该把机会掌握的自己的手里,这样我们才能去迎接改变自己的机会。不在多说了,我们还是看看代码吧:
android.permission.CHANGE_NETWORK_STATE接下来的许可:“AndroidManifest.xml”文件应该看起来如下:
Java代码:
- < ?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>
现在我们的权限已经设置好了,就得看看主要都到了什么包。
Java代码:
- package eoe.signal;
-
- 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;
现在我们就来看看代码吧
Java代码:
- public class GetGsmSignalStrength extends Activity
- {
-
- /* 我们可以用它们onResume和onPause方法停止listene*/
-
- 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);
-
- }
-
-
-
- /* —————————– */
-
- /* 开始PhoneState听众*/
-
- /* —————————– */
-
- private class MyPhoneStateListener extends PhoneStateListener
-
- {
-
- /* 从得到的信号强度,每个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();
-
- }
-
-
- };
-
-
-
- }