问题
I have a class that implements a broadcast receiver. I also inside of this class i want to be able to send a text message out automatically. Is this possible. Ive tried a lot of different things and nothing seems to work. Maybe im doing something wrong. But here is my source code that i have so far.
public class smsReceiver extends BroadcastReceiver {
private MainActivity main;
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, smsReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
Bundle bundle = intent.getExtras();
String str = "";
String phonenumber = "";
String houseNumber ="22";
String message = "Two bedrooms Two Baths";
SmsMessage []msgs = null;
SmsManager sms = SmsManager.getDefault();
if(bundle != null){
Object[]pdus = (Object[])bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i2=0; i2<msgs.length; i2++){
msgs[i2]= SmsMessage.createFromPdu((byte[])pdus[i2]);
phonenumber += msgs[i2].getOriginatingAddress();
str += msgs[i2].getMessageBody().toString();
}
sms.sendTextMessage(phonenumber, null, message, pi, null);
}
}
}
If i am doing anything wrong please let me know. Thanks everyone in advance.
Logcat errors
06-26 15:49:57.357: ERROR/ContactsProvider(175): Cannot determine the default account for contacts compatibility
06-26 15:49:57.357: ERROR/ContactsProvider(175): android.accounts.AuthenticatorException: bind failure
06-26 15:49:57.357: ERROR/ContactsProvider(175): at android.accounts.AccountManager.convertErrorToException(AccountManager.java:1437)
06-26 15:49:57.357: ERROR/ContactsProvider(175): at android.accounts.AccountManager.access$400(AccountManager.java:138)
06-26 15:49:57.357: ERROR/ContactsProvider(175): at android.os.Binder.execTransact(Binder.java:320)
06-26 15:49:57.357: ERROR/ContactsProvider(175): at dalvik.system.NativeStart.run(Native Method)
There is all my errors.
回答1:
Does your AndroidManifest.xml include the SEND_SMS permission?
EDIT: try this and see if it prints anything out:
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, smsReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
Bundle bundle = intent.getExtras();
String str = "";
String phonenumber = "";
String houseNumber ="22";
String message = "Two bedrooms Two Baths";
try{
SmsMessage []msgs = null;
SmsManager sms = SmsManager.getDefault();
if(bundle != null){
Object[]pdus = (Object[])bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i2=0; i2<msgs.length; i2++){
msgs[i2]= SmsMessage.createFromPdu((byte[])pdus[i2]);
phonenumber += msgs[i2].getOriginatingAddress();
str += msgs[i2].getMessageBody().toString();
}
sms.sendTextMessage(phonenumber, null, message, pi, null);
}
}catch(Exception e1){
android.util.Log.v("SMS ERROR","Exception sending SMS ["+e1.getMessage()+"]", e1);
}
}
回答2:
Put this block inside of your broadcast receiver file.
String phonenumber = "123456789";
String message = "Message";
SmsMessage []msgs = null;
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phonenumber, null, message, null, null);
You'll need to place this next to your imports in the same file:
import android.telephony.gsm.SmsManager;
You will also need this permission in your manifest:
<uses-permission android:name="android.permission.SEND_SMS" />
I hope this helps!
来源:https://stackoverflow.com/questions/6484807/how-to-send-sms-message-inside-broadcast-receiver-class