2013-05-10 125 views
5

我知道这里还有另外一个问题,但我不认为它适用于我,因为我很确定我使用GSM(isGSM()返回true)。无论如何,getCdmaDbm无论如何都会返回-1。我使用的是Android 4.1.1,并是HTC One X.这里是我的代码(其中大部分是不是我的):getGSMSignalStrength()总是返回99

MainActivity:

package com.example.receptionlookup; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.telephony.PhoneStateListener; 
import android.telephony.SignalStrength; 
import android.telephony.TelephonyManager; 
import android.view.Menu; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

    TelephonyManager  Tel; 
    MyPhoneStateListener MyListener; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     /* Update the listener, and start it */ 
     MyListener = new MyPhoneStateListener(); 
     Tel  = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
     Tel.listen(MyListener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    /* 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); 
    } 

    /* —————————– */ 
    /* Start the PhoneState listener */ 
    /* —————————– */ 
    private class MyPhoneStateListener extends PhoneStateListener 
    { 
     /* Get the Signal strength from the provider, each tiome there is an update */ 
     @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 */ 

} 

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.receptionlookup" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.receptionlookup.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/> 
</manifest> 

不任何人都知道问题是什么?如果我进入设置 - >关于 - >网络,我可以看到那里的信号强度。没有一些方法可以读取这个值吗?我已经尝试了几个第三方应用程序,但他们都不能读取我的信号强度。我也试过专有的getGSMSignalBar()方法,但是我得到了一个N​​oSuchMethodException。

+0

所以真的没有办法,我在信号强度编程看我的手机上? – 2013-05-11 15:04:05

+0

我可以使用其他方法吗? – 2013-05-12 15:05:25

+0

您是否尝试过任何其他设备上的代码?或者只是HTC One X? – Nate 2013-05-14 02:42:10

回答

0

正如你可以在3GPP 127 007 8.5中看到的,at+csq的实现是可选的(假设给出信号强度的命令)。显然HTC从第三方应用程序隐藏了这个值,他们可能有另一种方法来实现这个价值,以在他们自己的专有设置应用程序中显示。

事实上,其他应用程序也无法获得该信息证明我的情况。

This issue与你的紧密相关 - 他表示HTC是不值得现代相关开发时间的OEM之一。

+0

问题是,他们会怎么做? – 2013-05-18 03:18:33

+0

据我所知你不能这样做,有一个htc和调制解调器供应商之间的私人通信在一个名为'libsec-ril'的本地库中实现。这不是谷歌的,也不是开源的。 – Bush 2013-05-18 07:27:41

0

试试这个:

Class signalStrengthClass = signalStrength.getClass(); 
      try { 
       Method method = signalStrengthClass.getMethod(
         "getGsmSignalBar", null); 
       method.setAccessible(true); 
       Integer bars = (Integer) method.invoke(signalStrength, 
         (Object[]) null); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
+0

我得到一个NoSuchMethodException对此。 – 2013-05-18 13:38:20

+0

好的,如果用'getDeclaredMethod()'替换'getMethod(...)'会怎么样? – Bush 2013-05-18 17:19:04

+0

不幸的是,同样的结果。我知道这种方法适用于Galaxy S2。也许这只是HTC的一种方法。 – 2013-05-18 21:15:23