80,351
社区成员
发帖
与我相关
我的任务
分享
package com.zzy.eqclient.service;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.CellLocation;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
import com.zzy.eqclient.util.ShareUtil;
public class StrengthService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
final TelephonyManager telephonyManager = (TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new MyPhoneListener(), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("服务被销毁了……");
}
// @Override
// public int onStartCommand(Intent intent, int flags, int startId) {
// return super.onStartCommand(intent, flags, startId);
// }
private class MyPhoneListener extends PhoneStateListener {
@Override
public void onCellLocationChanged(CellLocation location) {
if (location instanceof GsmCellLocation) {
int cid = ((GsmCellLocation) location).getCid();
Log.i("获取到CID:", cid + "");
ShareUtil.putInt(getApplicationContext(), "cid", cid);
} else if (location instanceof CdmaCellLocation) {
int id = ((CdmaCellLocation) location).getBaseStationId();
Log.i("获取到ID:", id + "");
ShareUtil.putInt(getApplicationContext(), "id", id);
}
}
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
//这个ltedbm 是4G信号的值
String signalinfo = signalStrength.toString();
String[] parts = signalinfo.split(" ");
int dbm = Integer.parseInt(parts[9]);
int asu = 140 + dbm;
if(asu == 0) {
//这个dbm 是2G和3G信号的值
asu = signalStrength.getGsmSignalStrength();
dbm = -113 + 2 * asu;
}
ShareUtil.putInt(getApplicationContext(), "dbm", dbm);
ShareUtil.putInt(getApplicationContext(), "asu", asu);
Log.i("获取到DBM:", dbm + "\n" + "获取到ASU:" + asu + "");
super.onSignalStrengthsChanged(signalStrength);
}
}
}