我正在尝试检查服务是否正在运行。如果正在运行,我确实要发送短信。不幸的是,该服务在服务未运行时崩溃,并且在服务运行时未发送短信。
Logcat:
E / SmsReciever:示例smsReceiverjava.lang.NullPointerException
CallHelper类中的stop方法:
public boolean stop() {
tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
// mainActivity.toggleUI();
return false;
}
public class IncomingSms extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
private CallHelper callHelper;
@Override
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
if(callHelper.stop()) {
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
String sms = "TEST";
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(senderNum, null, sms, null, null);
Toast.makeText(context, "Sms sent Succesfully", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Sms Failed", Toast.LENGTH_LONG).show();
}
Log.i("SmsReciver", "senderNum: " + senderNum + "; message: " + message);
// Show Alert
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, "senderNum: " + senderNum + ", message: " + message, duration);
toast.show();
} // End For loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReciever", "Exeption smsReceiver" + e);
}
}
}
}
更新
初始化callhelper之后:
CallHelper callHelper = new CallHelper();
现在在我的stop方法上给了我一个nullpointerException。
我不知道为什么。这是一个返回true的布尔方法。
CallHelper类:
public class CallHelper {
public Boolean calling = false;
private String phoneNumber ="0";
private String lastNumber;
private int count=0;
public CallHelper() {
}
// Listener to detect incoming calls.
public class CallStateListener extends PhoneStateListener{
@Override
public void onCallStateChanged (int state, String incomingNumber) {
switch (state){...}
private Context ctx;
private TelephonyManager tm;
private CallStateListener callStateListener;
private MainActivity mainActivity;
public CallHelper(Context ctx){
this.ctx = ctx;
callStateListener = new CallStateListener();
}
public void SendMessage(){...}
// Start Call Detection.
public void start() {
tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
// Stop Call Dectection
public boolean stop() {
tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
// mainActivity.toggleUI();
return true;
}
}
更新的IncomingSMS类
公共类IncomingSms扩展了BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
private CallDetectService callDetectService;
public IncomingSms(){}
@Override
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
callDetectService = new CallDetectService();
Intent intent1 = new Intent(context,CallDetectService.class);
if (!callDetectService.stopService(intent1)) {
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
String sms = "TEST";
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(senderNum, null, sms, null, null);
Toast.makeText(context, "Sms sent Succesfully", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Sms Failed", Toast.LENGTH_LONG).show();
}
Log.i("SmsReciver", "senderNum: " + senderNum + "; message: " + message);
// Show Alert
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, "senderNum: " + senderNum + ", message: " + message, duration);
toast.show();
} // End For loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReciever", "Exeption smsReceiver" + e);
}
}
}
}
最佳答案
您的例外情况可能发生在
final Object[] pdusObj = (Object[]) bundle.get("pdus");
听说包不应该包含任何带有键“ pdus”的对象,因此pdusObj将保持为空,并且在for循环中,您尝试通过使用“ pdusObj.length”来获取长度,并且正在获取java.lang.NullPointerException。