3

信号强度始终显示 0

此代码运行没有错误,但始终显示信号强度为 0。我尝试在我的模拟器和我的 android 设备上运行此代码,但它显示信号强度为 0。

请详细说明获取我的移动网络的正确信号强度以及获取该地区其他网络信号强度的代码(如果可能)。

MainActivity.java

 package com.example.termproject;
 import android.os.Bundle;
 import android.view.Menu;
 import android.view.View;
 import android.app.Activity;
 import android.telephony.PhoneStateListener;
 import android.telephony.SignalStrength;
 import android.telephony.TelephonyManager;
 import android.content.Context;
 import android.widget.Toast;

 public class MainActivity extends Activity 
 {
    TelephonyManager Tel;
    MyPhoneStateListener MyListener;
    private int signal;

     @Override
     public void onCreate(Bundle savedInstanceState)
     {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

     }

     @Override
               public boolean onCreateOptionsMenu(Menu menu) 
     {
         getMenuInflater().inflate(R.menu.activity_main, menu);
         return true;
     }
     class MyPhoneStateListener extends PhoneStateListener
     {
       int signal;
       @Override
       public void onSignalStrengthsChanged(SignalStrength signalStrength)
       {
          super.onSignalStrengthsChanged(signalStrength);
          signal = signalStrength.getGsmSignalStrength();
       }
     }
     public void myaction(View v)
     {
        MyListener = new MyPhoneStateListener();
         Tel = ( TelephonyManager )getSystemService(Context.TELEPHONY_SERVICE);
         Tel.listen(MyListener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
         Toast.makeText(getApplicationContext(), "GSM signal strength is " +    String.valueOf(this.signal), Toast.LENGTH_SHORT).show();
     }
 }

清单.xml

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.termproject"
     android:versionCode="1"
     android:versionName="1.0" >

     <uses-sdk
         android:minSdkVersion="8"
         android:targetSdkVersion="15" />
     <!-- PERMISSIONS -->
    <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


    <!-- LICENSE PERMISSIONS -->    
    <uses-permission android:name="com.android.vending.CHECK_LICENSE" />

    <!-- USES -->
    <uses-feature android:name="android.hardware.telephony"      android:required="false"/>
     <application
         android:icon="@drawable/ic_launcher"
         android:label="@string/app_name"
         android:theme="@style/AppTheme" >
         <activity
             android:name=".MainActivity"
             android:label="@string/title_activity_main" >
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />

                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
     </application>

 </manifest>

activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent" >

     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerHorizontal="true"
         android:layout_centerVertical="true"
         tools:context=".MainActivity" />
     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/button_send" 
         android:onClick="myaction"/>


 </RelativeLayout>
4

1 回答 1

2

该线程似乎确定您的 PhoneStateListener 不正确。从您的问题来看,您似乎有一个 CDMA 设备,因此 GSM 将始终返回 0。不确定模拟器的预期行为是什么。

检测接收何时返回 Android

package com.piusvelte.wapdroid;

import static com.piusvelte.wapdroid.WapdroidService.PHONE_TYPE_CDMA;
import static android.telephony.NeighboringCellInfo.UNKNOWN_RSSI;
import android.telephony.CellLocation;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;

// PhoneStateListener for api >= 7
public class PhoneListenerApi7 extends PhoneStateListener {
        private WapdroidService mService;

        public PhoneListenerApi7(WapdroidService service) {
                mService = service;
        }

        public void onCellLocationChanged(CellLocation location) {
                // this also calls signalStrengthChanged, since signalStrengthChanged isn't reliable enough by itself
                mService.getCellInfo(location);
        }

        public void onSignalStrengthChanged(int asu) {
                // add cdma support, convert signal from gsm
                if ((mService.mTeleManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) || (mService.mTeleManager.getPhoneType() == PHONE_TYPE_CDMA)) mService.signalStrengthChanged((asu > 0) && (asu != UNKNOWN_RSSI) ? (2 * asu - 113) : asu);
                else mService.release();
        }

        public void onSignalStrengthsChanged(SignalStrength signalStrength) {
                if (mService.mTeleManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
                        int asu = signalStrength.getGsmSignalStrength();
                        mService.signalStrengthChanged((asu > 0) && (asu != UNKNOWN_RSSI) ? (2 * asu - 113) : asu);
                }
                else if (mService.mTeleManager.getPhoneType() == PHONE_TYPE_CDMA) mService.signalStrengthChanged(signalStrength.getCdmaDbm() < signalStrength.getEvdoDbm() ? signalStrength.getCdmaDbm() : signalStrength.getEvdoDbm());
                else mService.release();
        }
}
于 2012-10-12T12:39:50.030 回答