Instantly share code, notes, and snippets.

Embed
What would you like to do?
電波強度取得
package jp.sumasu.test;
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;
public class PhoneStateTestActivity extends Activity {
TelephonyManager telephonyManager;
TextView textView;
/**
* 電波強度の変更を検出するListner
*/
private PhoneStateListener phoneState = new PhoneStateListener() {
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
// とりあえず全部列挙
StringBuilder log = new StringBuilder();
log.append(String.format("isGsm = %b\n", signalStrength.isGsm()));
log.append(String.format("describeContents = %d\n",
signalStrength.describeContents()));
log.append(String.format("CDMA Dbm = %d\n",
signalStrength.getCdmaDbm()));
log.append(String.format("CDMA Ecio = %d\n",
signalStrength.getCdmaEcio()));
log.append(String.format("Evdo Dbm = %d\n",
signalStrength.getEvdoDbm()));
log.append(String.format("Evdo Ecio = %d\n",
signalStrength.getEvdoEcio()));
log.append(String.format("Evdo Snr = %d\n",
signalStrength.getEvdoSnr()));
log.append(String.format("GSM Bit Error Rate = %d\n",
signalStrength.getGsmBitErrorRate()));
log.append(String.format("GSM Signal Strength = %d\n",
signalStrength.getGsmSignalStrength()));
super.onSignalStrengthsChanged(signalStrength);
textView.setText(log.toString());
};
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.test);
telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
}
@Override
public void onResume() {
super.onResume();
telephonyManager.listen(phoneState,
PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
@Override
public void onPause() {
super.onPause();
telephonyManager.listen(phoneState, PhoneStateListener.LISTEN_NONE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment