Java getGSMSignalStrength()始终返回99
我知道这里还有一个与此相关的问题,但我不认为它适用于我,因为我非常确定我使用GSM(isGSM()返回true)。无论如何,getCdmaDbm都会为我返回-1。我正在使用Android 4.1.1和HTC One X。以下是我的代码(大部分不是我的): 主要活动:Java getGSMSignalStrength()始终返回99,java,android,signal-strength,Java,Android,Signal Strength,我知道这里还有一个与此相关的问题,但我不认为它适用于我,因为我非常确定我使用GSM(isGSM()返回true)。无论如何,getCdmaDbm都会为我返回-1。我正在使用Android 4.1.1和HTC One X。以下是我的代码(大部分不是我的): 主要活动: package com.example.receptionlookup; import android.os.Bundle; import android.app.Activity; import android.content.
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()方法,但我得到了一个NoSuchMethodException。正如您在文章中看到的那样,at+csq
的实现是可选的(该命令假定提供信号强度)。显然,HTC对第三方应用程序隐藏了该值,他们可能有另一种方法来实现该值,以便在自己的专有设置应用程序中显示
其他应用程序也无法获得这些信息,这一事实证明了我的理由
与您的密切相关-thay说HTC是不值得花费与调制解调器相关的开发时间的原始设备制造商之一。尝试以下方法:
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();
}
那么,我真的没有办法在手机上通过编程方式查看信号强度吗?有没有其他方法可以使用?我和你有类似的问题。对我来说,问题是我有一个欧洲设备(WCDMA),我试图测量美国设备(CDMA)的信号。getCdmaDbm()用于CDMA设备,如果您有WCDMA设备,它将无法工作并返回-1您是否在任何其他设备上尝试过该代码?或者仅仅是HTC One X?我有一个朋友尝试了相同的代码(除了getCdmaDbm而不是GSM),这对他有效。问题是,他们会怎么做?据我所知,你无法做到这一点,HTC和调制解调器供应商之间有一个私人通信,在一个名为“libsec ril”的本地库中实现。这不是谷歌的,也不是开源的。我得到一个NoSuchMethod异常。好吧,如果你用
getDeclaredMethod()
替换getMethod(…)
,结果很不幸。我知道这个方法对S2星系有效。也许这不是HTC的方法。