我想创建一个带有自定义线程ID的SMS,例如“ 10001”。我怎样才能做到这一点 ?原因是因为我需要实现删除SMS功能,并且删除特定SMS线程的唯一方法是通过线程ID或电话号码,并且此时无法准确获取电话号码,因此需要定义自定义线程ID在我发送短信时。
到目前为止,我只能获得正常的SMS工作代码,如下所示:
SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage("+1 203 514 6584", null, "HI Greg! ", null, null);
在此先感谢您的帮助!
的SmsObserver类是ContentObserver该寄存器本身上的content://sms/ Uri反对接收者的地址和消息主体中的SMS表并检查改变检索对于呼出SMS消息所分配的线程ID。该类提供了您的发送类需要实现的接口,以便在确定线程ID时接收它,因为这将异步发生。
SmsObserver
ContentObserver
content://sms/
Uri
public class SmsObserver extends ContentObserver { private static final Handler handler = new Handler(); private static final Uri uri = Uri.parse("content://sms/"); private final Context context; private final ContentResolver resolver; private final String address; private final String body; public interface OnSmsSentListener { public void onSmsSent(int threadId); } public SmsObserver(Context context, String address, String body) { super(handler); if (context instanceof OnSmsSentListener) { this.context = context; this.resolver = context.getContentResolver(); this.address = address; this.body = body; } else { throw new IllegalArgumentException( "Context must implement OnSmsSentListener interface"); } } public void start() { if (resolver != null) { resolver.registerContentObserver(uri, true, this); } else { throw new IllegalStateException( "Current SmsObserver instance is invalid"); } } @Override public void onChange(boolean selfChange, Uri uri) { Cursor cursor = null; try { cursor = resolver.query(uri, null, null, null, null); if (cursor != null && cursor.moveToFirst()) { final int type = cursor.getInt( cursor.getColumnIndex(Telephony.Sms.TYPE)); if(type == Telephony.Sms.Sent.MESSAGE_TYPE_SENT) { final String address = cursor.getString( cursor.getColumnIndex(Telephony.Sms.ADDRESS)); final String body = cursor.getString( cursor.getColumnIndex(Telephony.Sms.BODY)); final int threadId = cursor.getInt( cursor.getColumnIndex(Telephony.Sms.THREAD_ID)); if (PhoneNumberUtils.compare(address, this.address) && body.equals(this.body)) { ((OnSmsSentListener) context).onSmsSent(threadId); resolver.unregisterContentObserver(this); } } } } finally { if (cursor != null) { cursor.close(); } } } }
在发送消息之前,需要启动此实例,并将线程ID传递到发送类的接口方法实现中。例如,如果您是Activity通过点击发送来自的Button:
Activity
Button
public class MainActivity extends Activity implements SmsObserver.OnSmsSentListener { ... public void onClick(View v) { String address = "+1 234 567 8900"; String body = "HI Greg! "; new SmsObserver(this, address, body).start(); SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(address, null, body, null, null); } @Override public void onSmsSent(int threadId) { // Here's the thread ID. } }
请注意,您还将需要获得READ_SMS许可。
READ_SMS
从棒棒糖开始可能的替代方法。所发送的消息的URI将作为被附接String额外的Intent从PendingIntent通过如在第四个参数sendTextMessage()的方法。额外的将具有key "uri",并且可以将其解析为Uri,然后可以在查询中使用它ContentResolver来检索线程ID,如上所示。
String
Intent
PendingIntent
sendTextMessage()
"uri"
ContentResolver
例如,如果使用a BroadcastReceiver作为结果,则sendTextMessage()调用将如下所示:
BroadcastReceiver
Intent sentIntent = ... PendingIntent sentPi = PendingIntent.getBroadcast(context, 0, sentIntent, 0); SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(address, null, body, sentPi, null);
并在Receiver中检索额外的内容是这样的:
public class SmsResultReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { ... String uriString = data.getStringExtra("uri"); Uri uri = Uri.parse(uriString); // Query as shown above in the ContentObserver ... } }