I am trying to capture incoming sms messages for my application. In order to do that I am using a BroadcastReceiver. I have setup permissions and a filter in the manifest file. Whenever I receive a text the application crashes, when I try to debug the program it doesn't even get to the first line of code in the receiver. Does anyone know what I am doing wrong?
Here is the code for the receiver:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.os.Bundle;
public class SMSReceiver extends BroadcastReceiver {
String from = null;
String msg = null;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
this.abortBroadcast();
开发者_JAVA技巧 //---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
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]);
str += "SMS from " + msgs[i].getOriginatingAddress();
from = msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
msg = msgs[i].getMessageBody().toString();
str += "\n";
}
}
}
}
Remove Bin & Gen File form your project & also register this receiver in your mainfest.
<receiver android:name="yourpackagename.Boot" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_CB_RECEIVED" >
</action>
<action android:name="android.provider.Telephony.SMS_RECEIVED" >
</action>
</intent-filter>
</receiver>
I hope its work for you because its work for me.
精彩评论