我正在尝试获取我连接的当前单元的信号强度。到目前为止,我已经找到了这个例子:
http://www.android10.org/index.php/forums/44-maplocation/868-tutorialget-gsm-signal
但这只是为了在信号改变时得到一个 Toast。有没有办法将信号强度的值保存在变量中?
我使用以下代码尝试了此操作,方法是将值保存在变量上并获取它,但我一直在获取“初始值”。这是代码:
package org.mode.gsm;
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.TextView;
import android.widget.Toast;
public class GetGsmSignalStrength extends Activity {
TelephonyManager Tel;
MyPhoneStateListener MyListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView abcd = new TextView(this);
abcd.setText(getRSSI());
setContentView(abcd);
}
public String getRSSI() {
MyListener = new MyPhoneStateListener();
Context context = (Context) getApplicationContext();
Tel = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
Tel.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
String GSMCinr = "GSM CINR = " + MyListener.getCinr();
return GSMCinr;
}
private class MyPhoneStateListener extends PhoneStateListener {
String cinr = "initial value";
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
cinr = String.valueOf(signalStrength.getGsmSignalStrength());
Toast.makeText(
getApplicationContext(),
"GSM Cinr = "
+ String.valueOf(signalStrength
.getGsmSignalStrength()), Toast.LENGTH_LONG)
.show();
}
public String getCinr() {
return cinr;
}
}
}
和清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.mode.gsm"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".GetGsmSignalStrength" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
如果有人可以帮助我保存信号强度的值,我将非常感激,或者建议任何其他方式来做到这一点......
谢谢