【问题标题】:How to read the message content of a new in coming message in android?如何在android中读取新消息的消息内容?
【发布时间】:2011-02-15 17:45:44
【问题描述】:

我想以编程方式在 android 中读取新传入 SMS 的消息正文。

我尝试了一些东西,但没有返回任何内容:

Uri uri = Uri.parse("content://sms/inbox");
        ContextWrapper context = null;      
        Cursor c = context.getContentResolver().query(uri, null, null ,null,null);      
        String body = null; 
        String number=null;
        if(c.moveToFirst()) {        
           body = c.getString(c.getColumnIndexOrThrow("body")).toString();
           number = c.getString(c.getColumnIndexOrThrow("address")).toString();
        }
        c.close(); 

【问题讨论】:

标签: android sms


【解决方案1】:

我已经在我的班级网站上发布了一些关于此的示例程序。 这是示例Read SMS Example 这是一个sn-p的代码。基本上你可以注册一个广播接收器来监听 SMS_Receive 并查看以下内容。

Intent intent = getIntent();
    Bundle bundle = intent.getBundleExtra("mySMS");

    if (bundle != null) {
        Object[] pdus = (Object[])bundle.get("pdus");
        SmsMessage sms = SmsMessage.createFromPdu((byte[])pdus[0]);
        Log.i("mobile.cs.fsu.edu", "smsActivity : SMS is <" +  sms.getMessageBody() +">");

        //strip flag
        String message = sms.getMessageBody();
        while (message.contains("FLAG"))
            message = message.replace("FLAG", "");

        TextView tx = (TextView) findViewById(R.id.TextBox);
        tx.setText(message);            
    } else
        Log.i("mobile.cs.fsu.edu", "smsActivity : NULL SMS bundle");

【讨论】:

  • 还有一个静态的 getMessagesFromIntent(Intent) 方法返回一个 SmsMessage-array(从 API 级别 19 开始),因此如果您的目标可能不需要手动读取/转换“pdus”字节数组级别足够高:developer.android.com/reference/android/provider/…
  • 我的手机里有 2 个模拟人生!是否可以检查哪个 sim 正在接收消息?
【解决方案2】:

下面是读取传入消息并显示在列表视图中的一段代码,不要忘记在清单文件中添加权限:

<uses-permission android:name="android.permission.READ_SMS"/>

代码如下:

 listitem=(ListView)findViewById(R.id.ListView);

        Uri mSmsQueryUri = Uri.parse("content://sms/inbox");
        List<String> messages = new ArrayList<String>();

        Cursor cursor = null;
        try {
            cursor = getContentResolver().query(mSmsQueryUri, null, null, null, null);
            if (cursor == null) {
                Log.i(TAG, "cursor is null. uri: " + mSmsQueryUri);

            }
            for (boolean hasData = cursor.moveToFirst(); hasData; hasData = cursor.moveToNext()) {
                final String body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
                messages.add(body);
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        } finally {
            cursor.close();
        }

        listitem.setAdapter(new ArrayAdapter<String>(ReadMessage.this, android.R.layout.simple_list_item_1,messages));

【讨论】:

  • 有没有办法只收到今天的消息而不是接收所有的消息?
【解决方案3】:

一个非常简单的解决方案是使用这个 SMS Parser 库:

https://github.com/adorsys/sms-parser-android

compile 'de.adorsys.android:smsparser:0.0.3'

使用它,您可以阅读整条消息,也可以阅读传入消息的特定部分。您还可以设置接收消息的电话号码。

如果您需要有关它的工作原理或用法的更多信息,请查看我上面列出的 github 存储库。

【讨论】:

    【解决方案4】:

    在此示例中,我将向您展示如何从收件箱中读取最近收到(传入)的短信并在 textview 中显示。

     fstmsgBtn.setOnClickListener(new OnClickListener()
    
        { public void onClick(View v) 
        {
            Uri my_uri = Uri.parse("content://sms/inbox");          
            Cursor readFstSms =v.getContext().getContentResolver().query(my_uri, null, null ,null,null); 
            if(readFstSms.moveToFirst()) 
            {
               String  msg_body =  c.getString(c.getColumnIndexOrThrow("body")).toString();
               //String sender_number = c.getString(c.getColumnIndexOrThrow("address")).toString();
               readtxt.setText(msg_body);
            }
            readFstSms.close();
        }
        });
    

    【讨论】:

      【解决方案5】:
      listitem=(ListView)findViewById(R.id.list_view);
      
                  Uri mSmsQueryUri = Uri.parse("content://sms/inbox");
                  List<String> messages = new ArrayList<String>();
      
                  Cursor cursor = null;
                  try {
                      cursor = getContentResolver().query(mSmsQueryUri, null, null, null, null);
                      if (cursor == null) {
                         // Log.i(TAG, "cursor is null. uri: " + mSmsQueryUri);
      
                      }
                      for (boolean hasData = cursor.moveToFirst(); hasData; hasData = cursor.moveToNext()) {
                          final String body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString();
                          final String sender_no= cursor.getString(cursor.getColumnIndexOrThrow("address")).toString();
                          final String date= cursor.getString(cursor.getColumnIndexOrThrow("date"));
                          final String type =cursor.getString(cursor.getColumnIndexOrThrow("type"));
      
      
                          messages.add(body);
                          messages.add(sender_no);
                          messages.add(date);
                          messages.add(type);
                      }
                  } catch (Exception e) {
                      //Log.e(TAG, e.getMessage());
                  } finally {
                      cursor.close();
                  }
      
                  listitem.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,messages));
           }
      }
      

      【讨论】:

        猜你喜欢
        • 2018-11-19
        • 1970-01-01
        • 1970-01-01
        • 2020-09-05
        • 1970-01-01
        • 2021-12-13
        • 2021-06-27
        • 2020-03-01
        • 2015-06-03
        相关资源
        最近更新 更多