我写了一个简单的代码来监听onSignalStrengthsChanged:
package ir.yadgari.myservingcell;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
public class MainActivity extends AppCompatActivity {
TelephonyManager Tel;
Integer mCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Tel.listen(new mListener(), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
class mListener extends PhoneStateListener
{
@Override
public void onSignalStrengthsChanged(SignalStrength sStrength)
{
TextView tvStrength = (TextView) findViewById(R.id.tvStrength);
TextView tvCount = (TextView) findViewById(R.id.tvCount);
tvStrength.setText(sStrength.toString());
mCount++;
tvCount.setText("Activity runs for " + String.valueOf(mCount) + " times");
//showMyMessage(String.valueOf(sStrength.getGsmSignalStrength()));
}
}
}
方法" onSignalStrengthsChanged"只在第一次运行,而不是在网络信号变化时运行。
注意:我搜索了互联网,发现了很多关于此的内容,但没有解决我的问题。
答案 0 :(得分:0)
对我而言,请使用以下代码。
package com.example.h0136899.phonestrengthchange;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Context;
import android.widget.TextView;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
public class MainActivity extends AppCompatActivity {
TextView textview = null;
TextView textview1 = null;
TelephonyManager Tel;
int mCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview = (TextView) findViewById(R.id.textView);
textview1 = (TextView) findViewById(R.id.textView);
Tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Tel.listen(new mListener(), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
class mListener extends PhoneStateListener
{
@Override
public void onSignalStrengthsChanged(SignalStrength sStrength)
{
textview.setText(sStrength.toString());
mCount++;
textview1.setText("Activity runs for " + String.valueOf(mCount) + " times");
//showMyMessage(String.valueOf(sStrength.getGsmSignalStrength()));
}
}
}