如何在android中读取新消息中的消息内容?
问题描述:
任何人都可以帮助我吗?如何在android中读取新消息中的消息内容?
我想以编程方式阅读Android中新传入短信的消息正文。
我试过的东西,但不返回任何内容:
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();
是我的代码中的任何错误 请任何人给我答案,请完全是因为我浪费了一个月的时间这个
答
我已经在我的班级网站上发布了一些关于此的示例程序。 以下是示例Read SMS Example 以下是一段代码。基本上你可以注册一个广播接收器来收听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");
+0
工作很好谢谢弗兰克 – Krishna 2011-06-20 14:56:53
答
下面是一段代码,其读取在列表视图中的传入消息和显示,不要忘记添加权限清单文件:
<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));
答
在这个例子中,我将向您演示如何从收件箱中读取最近收到的(传入)短信,并在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();
}
});
答
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));
}
}
答
一个非常简单的解决办法是使用SMS解析库:
https://github.com/adorsys/sms-parser-android
compile 'de.adorsys.android:smsparser:0.0.3'
使用它,你可以阅读整个消息或具体部位传入消息。您还可以设置电子邮件的来电号码。
如果您需要更多关于它如何工作或使用情况的信息,请查看我上面列出的github存储库。
好文章在这里:http://mobdev.olin.edu/mobdevwiki/FrontPage/Tutorials/SMS%20Messaging – 2011-10-02 18:35:19
http://*.com/questions/14097500/get-inbox-messages-from-android-device在自定义列表视图# 它适合我。这个家伙在这里* – texeratx 2015-12-04 13:07:30