Android:使用 android.provider.Telephony.Sms.Conversations

标签 android sms android-contentprovider

我正在尝试在 android 上创建一个应用程序,我需要在其中像时尚一样在聊天中显示消息。我一直在阅读 SMS 的 4.4 API,但我似乎无法弄清楚如何使用它们。目前,我只收到来自 sms.Inbox 内容提供商的消息。 谁能告诉我在哪里可以看到一些有关如何有效使用它们的示例?

最佳答案

使用BroadcastReceiver在手机收到短信时进行广播,如下所示:

public class SmsReceiver extends BroadcastReceiver {
       @Override
       public void onReceive(Context context, Intent intent) {

              Bundle extras = intent.getExtras();
              if (extras == null)
                     return;

              // To display a Toast whenever there is an SMS.
              // Toast.makeText(context,"Recieved",Toast.LENGTH_LONG).show();

              Object[] pdus = (Object[]) extras.get("pdus");
              for (int i = 0; i < pdus.length; i++) {
                     SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
                     String sender = SMessage.getOriginatingAddress();
                     String body = SMessage.getMessageBody().toString();

                     // A custom Intent that will used as another Broadcast
                     Intent in = new Intent("SmsMessage.intent.MAIN").putExtra(
                                  "get_msg", sender + ":" + body);

                     // You can place your check conditions here(on the SMS or the
                     // sender)
                     // and then send another broadcast
                     context.sendBroadcast(in);

                     // This is used to abort the broadcast and can be used to silently
                     // process incoming message and prevent it from further being
                     // broadcasted. Avoid this, as this is not the way to program an
                     // app.
                     // this.abortBroadcast();
              }
       }

关于Android:使用 android.provider.Telephony.Sms.Conversations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24841978/

相关文章:

android - 如何在Android中绘制类似饼图的图表

java - Android 日程短信

android - 安全地限制对 FileProvider 的访问

android - 如何让在 Xamarin.Forms 项目的另一个类中创建的 Intent 可以访问 MainActivity?

android - Retrofit 2 没有 json 的请求体,只是一个普通的形式?

android - Android 中的消息和电子邮件 Intent ?

android - 通过 twilio 从 Android 应用程序发送和接收短信

android sqlite 数据库约束

android - CursorLoader 连接到错误的 ContentProvider

android - 无法将更新的 Android 应用程序上传到 Play 商店。以前的证书指纹是空白的