我正在开发一个应用程序,它会在收到短信时做出反应。 收到短信时,我的应用程序崩溃了但是由于必须在我自己的设备上安装应用程序,我无法获得任何崩溃报告以确定崩溃时的问题。
代码在使用android工作室构建之前没有显示错误。
public class ReceiveTextMessage extends BroadcastReceiver{
private SharedPreferences preferences;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle bundle = intent.getExtras(); //---get the SMS message passed in---
SmsMessage[] msgs = null;
String msg_from;
if (bundle != null){
//---retrieve the SMS message received---
try{
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
msg_from = msgs[i].getOriginatingAddress();
String msgBody = msgs[i].getMessageBody();
}
}catch(Exception e){
// Log.d("Exception caught",e.getMessage());
}
}
}
}
}
任何人都可以告诉我为什么会发生崩溃或者无论如何我都能找到崩溃报告。
我以不同的方式编辑了我的代码,所有这些都给出了相同的结果。
答案 0 :(得分:0)
发布我自己的运行代码:
public class SmsReceiver extends BroadcastReceiver {
static final String ACTION =
"android.provider.Telephony.SMS_RECEIVED";
@SuppressWarnings("deprecation")
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION)) {
Bundle pudsBundle = intent.getExtras();
Object[] pdus = (Object[]) pudsBundle.get("pdus");
SmsMessage messages = SmsMessage.createFromPdu((byte[]) pdus[0]);
String phoneNumber = messages.getOriginatingAddress();
if (phoneNumber != null && phoneNumber.contains(Constants.OTP_NUMBER)) {
this.abortBroadcast();
Log.i("ABORTED", "DONE");
Intent localIntent = new Intent(Constants.OTP_BROADCASTING_PACKAGE);
String otp = messages.getMessageBody();
if (otp != null && !otp.equals("")) {
otp = otp.substring(otp.length() - 5, otp.length() - 1);
localIntent.putExtra("message", otp);
LocalBroadcastManager.getInstance(context).sendBroadcast(localIntent);
}
}
}
}
}
**In Manifest**
<receiver android:name="your_path.SmsReceiver">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Permission :
<uses-permission android:name="android.permission.RECEIVE_SMS" />
希望它会对你有所帮助:)。
答案 1 :(得分:0)
试试这个
我希望您在receiver
文件和权限中注册了manifest
。
public class IncomingSms extends BroadcastReceiver {
// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
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();
Log.i("SmsReceiver", "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("SmsReceiver", "Exception smsReceiver" +e);
}
}
}