Android SMS history
We currently have an application that sends out SMS to users, it acts like a SMS gateway sitting on an Android phone.
I have come across a lot of questions on how to reads SMS, SMS threads etc, i was wondering if it is possible to figure out which thread and incoming SMS belongs to.
We are looking at automating where we send an SMS to user with an ORDER ID, when user replies back we would like to figure out which thread the SMS belongs to and try to extract the ORDER ID from tha开发者_如何学编程t SMS that initiated the conversation.
You can find out the thread id with the code below. But it may be even better just to query all the messages with the same address and it is outbound (Sms.TYPE == 2 i think). This way you can bypass the need for the thread id, and just start looking for the Order ID in one swoop.
import android.provider.Telephony.Sms;
import android.provider.Telephony.Sms.Intents;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.telephony.SmsMessage;
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intents.SMS_RECEIVED_ACTION)){
int threadId = 0;
SmsMessage[] messages = Intents.getMessagesFromIntent(intent);
String fromAddress = messages[0].getDisplayOriginatingAddress();
Cursor c = Sms.query(context.getContentResolver(),
new String[]{Sms.THREAD_ID},
Sms.ADDRESS+"="+fromAddress,
Sms.DATE+" DESC");
if(c.moveToNext()){
threadId = c.getInt(0);
}
c.close();
if(threadId!=0){
//query the Sms again, reading body for your Order ID, etc ...
}
}
}
}
精彩评论