如何在Android中使用带有自定义线程ID的SmsManager发送短信?

如何在Android中使用带有自定义线程ID的SmsManager发送短信?,第1张

如何在Android中使用带有自定义线程ID的SmsManager发送短信?

SmsObserver
类是
ContentObserver
该寄存器本身上的
content://sms/

Uri
反对接收者的地址和消息主体中的SMS表并检查改变检索对于呼出SMS消息所分配的线程ID。该类提供了您的发送类需要实现的接口,以便在确定线程ID时接收它,因为这将异步发生。

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

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
许可。


从棒棒糖开始可能的替代方法。所发送的消息的URI将作为被附接

String
额外的
Intent
PendingIntent
通过如在第四个参数
sendTextMessage()
的方法。额外的将具有key
"uri"
,并且可以将其解析为
Uri
,然后可以在查询中使用它
ContentResolver
来检索线程ID,如上所示。

例如,如果使用a

BroadcastReceiver
作为结果,则
sendTextMessage()
调用将如下所示:

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        ...    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5056623.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-16

发表评论

登录后才能评论

评论列表(0条)

保存