gpt4 book ai didi

android - 在服务中调用 PhoneStateListener 时出现 NullPointerExecption

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:18:26 25 4
gpt4 key购买 nike

我有一个 PhoneStateLister,我从中获取大量数据并将其发送到服务器。

如果我在 fragment 中调用监听器,一切都很好,但是当我在服务中调用它时,我得到一个 NullPointerException。这是我的代码:

电话状态监听器:

public class CarrierStateListener extends PhoneStateListener {

private Context context;
private TelephonyManager telephonyManager;
private String carrierName;
private int mSignalStrength = 0;
private int mCellTowerID = 0;
private int mCellTowerAreCode = 0;
private int mcc = 0;
private int mnc = 0;


public CarrierStateListener(Context context) {
this.context = context;
}

/**
* Get the Signal strength from the provider, each time there is an update
*/
@Override
public void onSignalStrengthsChanged(final SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);

telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation loc = (GsmCellLocation) telephonyManager.getCellLocation();
String networkOperator = telephonyManager.getNetworkOperator();

/** Retrieving the carrier name **/
carrierName = telephonyManager.getNetworkOperatorName();
UserInformation.getSingleton(context).setNetwork(carrierName);

/** Retrieving the RSSI value **/
mSignalStrength = signalStrength.getGsmSignalStrength();
mSignalStrength = (2 * mSignalStrength) - 113; // -> dBm
UserInformation.getSingleton(context).setRssi(String.valueOf(mSignalStrength));

/** Retrieve the cell tower id (CID) and the LAC (location area code) **/
mCellTowerID = loc.getCid();
mCellTowerAreCode = loc.getLac();
UserInformation.getSingleton(context).setCellId(String.valueOf(loc.getCid()));

/**tower mobile network code (mnc) and mobile country code (mcc) **/
if (networkOperator != null) {
mcc = Integer.parseInt(networkOperator.substring(0, 3));
mnc = Integer.parseInt(networkOperator.substring(3));
UserInformation.getSingleton(context).setMnc(String.valueOf(mnc));
UserInformation.getSingleton(context).setMcc(String.valueOf(mcc));

} else {
Toast.makeText(context, "Couldn't retrieve mnc and mcc because network operator is "
+ networkOperator, Toast.LENGTH_SHORT).show();
}


}

和我的服务:

public class SendDataService extends Service {

private static final String TAG = "SendDataService";
private Thread t;
private TelephonyManager telephonyManager;
private CarrierStateListener pslistener;

@Override
public IBinder onBind(Intent intent) {
return null;
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SendDataService");
wl.acquire();

t = new Thread() {
@Override
public void run() {
while (true) {
try {

Log.e(TAG, "sending data...");

//post request to send data
Thread.sleep(6000);

} catch (InterruptedException ie) {
}
}
}
};
t.start();

Log.e(TAG, "the SendDataService started");
return START_STICKY;
}

然后我在 fragment 的 onResume() 中启动服务:

getActivity().startService(new Intent(getActivity(), SendDataService.class));

当 fragment 开始时...这是我得到的异常:

09-14 16:11:05.034  21697-21697/? E/CallDataService﹕ the SendDataService started
09-14 16:11:05.036 21697-21729/? E/CallDataService﹕ sending data...
09-14 16:11:05.037 21697-21729/? E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-2445
Process: xxxxxxxx, PID: 21697
java.lang.NullPointerException: Attempt to read from field

'android.os.MessageQueue android.os.Looper.mQueue' on a null object reference
at android.os.Handler.<init>(Handler.java:229)
at android.os.Handler.<init>(Handler.java:137)
at android.telephony.PhoneStateListener$1.<init>(PhoneStateListener.java:270)
at android.telephony.PhoneStateListener.<init>(PhoneStateListener.java:270)
at android.telephony.PhoneStateListener.<init>(PhoneStateListener.java:240)
at xxxxxxxxxxxxx.CarierStateListener.<init>(CarierStateListener.java:27)
at xxxxxxxxx.SendDataService.listenForEvents(SendDataService.java:96)
at xxxxxx.util.SendDataService.access$000(SendDataService.java:25)
at xxxxxxxxxx.util.SendDataService$1.run(SendDataService.java:53)

最佳答案

这是一个消息队列问题,您必须在调用 telephonyManager.listen 之前初始化循环器并在之后循环。

 new Thread(new Runnable() {
@Override
public void run() {
quitLooper = false;
Looper.prepare();
tManager.listen(new PhoneStateListener() {
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
lastSignalStrength = signalStrength;
if(quitLooper)
Looper.myLooper().quit();

}
}, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);

Looper.loop();
}
}).start();

关于android - 在服务中调用 PhoneStateListener 时出现 NullPointerExecption,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32568328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com