SMSBroadCast,如何接收和输出我发送的短信?
SMSBroadCast, How do I receive and output SMS I have sent?
我有这个片段来接收传入的文本消息,并将它们显示为带有 Toast 的通知,并使用 writeToFile(); 将包含的发件人 Phone 和消息正文粘贴到一个文件中。如何接收我发送给其他人的短信?
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();
}
}
}
}
}
就像使用 Uri 从我的 "Sent" 框中获取内容一样简单。
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)
我有这个片段来接收传入的文本消息,并将它们显示为带有 Toast 的通知,并使用 writeToFile(); 将包含的发件人 Phone 和消息正文粘贴到一个文件中。如何接收我发送给其他人的短信?
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();
}
}
}
}
}
就像使用 Uri 从我的 "Sent" 框中获取内容一样简单。
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)