信号强度始终显示 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>