我有这个片段来接收传入的短信并将其显示为Toast的通知,并将包含发件人电话和邮件正文的内容粘贴到带有writeToFile();的文件中。如何收到发送给他人的短信?
public class SMSBroadcast extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private static final String TAG = "SMSBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Intent recieved: " + intent.getAction());
Toast.makeText(context, "Recieved", Toast.LENGTH_LONG).show();
if (SMS_RECEIVED.equals(intent.getAction())) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[])bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
}
if (messages.length > -1) {
Log.i(TAG, "Message recieved: " + messages[0].getMessageBody());
Toast.makeText(context, messages[0].getDisplayOriginatingAddress(), Toast.LENGTH_LONG).show();
try {
writeToFile( messages[0].getDisplayOriginatingAddress()+messages[0].getMessageBody());
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(context,e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
}
}
答案 0 :(得分:0)
就像使用Uri从我的“已发送”框中获取内容一样简单。
TextView view = new TextView(this);
Uri uriSMSURI = Uri.parse("content://sms/sent");
Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);
String sms = "";
while (cur.moveToNext()) {
sms += "To :" + cur.getString(2) + " : " + cur.getString(12)+"\n";
}
//Do code with string (sms)