0

I have a problem regarding Signal strength value.
I want to put the signal strength value inside the onCreate function but it gives me zero value. But when I test it inside PhoneStateListener it gives me a value which is 7. Any Idea? here is my code.

package com.example.activitylifecycle;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class ActivitySignal extends Activity {
  private static final String TAG = "ActivitySignal";
  private int signalAsu = 0;
  PhoneStateListener signalListener=new PhoneStateListener()
  {
    public void onSignalStrengthChanged(int asu)
    {
      Log.d(TAG, "SignalStrength: " + String.valueOf(asu)); // It gives me a value which is 7
      signalAsu = asu;
    }
  };

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  TelephonyManager SignalManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
  SignalManager.listen(signalListener,PhoneStateListener.LISTEN_SIGNAL_STRENGTH);
  //another log test
  Log.d(TAG, "SignalStrength: " + String.valueOf(signalAsu)); // zero value..
  }
}
4

1 回答 1

0

onCreate 已运行,并且您已在活动中将 signalAsu 设置为 0。因此,您的日志将显示为零,因为signalAsu尚未更改。忽略您的初始值为零,只听何时onSignalStrengthChanged调用,就像您处理日志一样。

于 2013-03-18T11:03:37.500 回答