Android 读取来自特定发件人的所有短信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10870230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read all SMS from a particular sender
提问by KalEl
How do I read all SMSes from a particular sender to me? E.g. I want to scan a) the body, and b) the date/time of all SMSes that came from 'TM-MYAMEX' to the phone.
如何读取来自特定发件人的所有短信给我?例如,我想扫描 a) 身体,以及 b) 从“TM-MYAMEX”发送到手机的所有 SMS 的日期/时间。
Some websites seem to indicate this can be read from "content://sms/inbox". I couldn't figure out exactly how. Also not sure if it is supported on most phones. I am using a Galaxy S2.
一些网站似乎表明这可以从“content://sms/inbox”中读取。我无法弄清楚究竟如何。也不确定它是否在大多数手机上受支持。我正在使用 Galaxy S2。
回答by ρяσ?ρ?я K
try this way:
试试这个方法:
StringBuilder smsBuilder = new StringBuilder();
final String SMS_URI_INBOX = "content://sms/inbox";
final String SMS_URI_ALL = "content://sms/";
try {
Uri uri = Uri.parse(SMS_URI_INBOX);
String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };
Cursor cur = getContentResolver().query(uri, projection, "address='123456789'", null, "date desc");
if (cur.moveToFirst()) {
int index_Address = cur.getColumnIndex("address");
int index_Person = cur.getColumnIndex("person");
int index_Body = cur.getColumnIndex("body");
int index_Date = cur.getColumnIndex("date");
int index_Type = cur.getColumnIndex("type");
do {
String strAddress = cur.getString(index_Address);
int intPerson = cur.getInt(index_Person);
String strbody = cur.getString(index_Body);
long longDate = cur.getLong(index_Date);
int int_Type = cur.getInt(index_Type);
smsBuilder.append("[ ");
smsBuilder.append(strAddress + ", ");
smsBuilder.append(intPerson + ", ");
smsBuilder.append(strbody + ", ");
smsBuilder.append(longDate + ", ");
smsBuilder.append(int_Type);
smsBuilder.append(" ]\n\n");
} while (cur.moveToNext());
if (!cur.isClosed()) {
cur.close();
cur = null;
}
} else {
smsBuilder.append("no result!");
} // end if
}
} catch (SQLiteException ex) {
Log.d("SQLiteException", ex.getMessage());
}
AndroidManifest.xml:
AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_SMS" />
回答by Tabish khan
try this, its a easy way for reading message from inbox.
试试这个,它是一种从收件箱中读取消息的简单方法。
public class ReadMsg extends AppCompatActivity {
ListView listView;
private static final int PERMISSION_REQUEST_READ_CONTACTS = 100;
ArrayList smsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_msg);
listView = (ListView)findViewById(R.id.idList);
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS);
if (permissionCheck == PackageManager.PERMISSION_GRANTED){
showContacts();
}else{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS},PERMISSION_REQUEST_READ_CONTACTS);
}
}
private void showContacts() {
Uri inboxURI = Uri.parse("content://sms/inbox");
smsList = new ArrayList();
ContentResolver cr = getContentResolver();
Cursor c = cr.query(inboxURI,null,null,null,null);
while (c.moveToNext()){
String Number = c.getString(c.getColumnIndexOrThrow("address")).toString();
String Body= c.getString(c.getColumnIndexOrThrow("body")).toString();
smsList.add("Number: "+ Number + "\n" + "Body: "+ Body );
}
c.close();
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, smsList);
listView.setAdapter(adapter);
}
}
}
Add this permission into Manifests.
将此权限添加到清单中。
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
回答by Anubhav
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inbox = (Button) findViewById(R.id.inbox);
list = (ListView) findViewById(R.id.list);
arlist = new ArrayList<String>();
inbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri inboxUri = Uri.parse("content://sms/inbox");
String[] reqCols = {"_id", "body", "address"};
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(inboxUri, reqCols, "address='+919456'", null, null);
adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.msg_content_layout, cursor,
new String[]{"body", "address"}, new int[]{R.id.txt1, R.id.txt2});
list.setAdapter(adapter);
}
});
}
Here I have added the number +919456 as the value of the address field.
这里我添加了数字 +919456 作为地址字段的值。
Apart from this you need to add the READ_SMS permission to manifest:
除此之外,您需要将 READ_SMS 权限添加到清单中:
回答by Adiii
you can do this in few line of code
你可以用几行代码做到这一点
String[] phoneNumber = new String[] { "+923329593103" };
Cursor cursor1 = getContentResolver().query(Uri.parse("content://sms/inbox"), new String[] { "_id", "thread_id", "address", "person", "date","body", "type" }, "address=?", phoneNumber, null);
StringBuffer msgData = new StringBuffer();
if (cursor1.moveToFirst()) {
do {
for(int idx=0;idx<cursor1.getColumnCount();idx++)
{
msgData.append(" " + cursor1.getColumnName(idx) + ":" + cursor1.getString(idx));
}
} while (cursor1.moveToNext());
} else {
edtmessagebody.setText("no message from this contact"+phoneNumber);
}
回答by 113408
public class SmsController extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
SmsMessage msgs[] = null;
Bundle bundle = intent.getExtras();
try {
Object pdus[] = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int n = 0; n < pdus.length; n++) {
byte[] byteData = (byte[]) pdus[n];
msgs[n] = SmsMessage.createFromPdu(byteData);
}
} catch (Exception e) {
}
for (int i = 0; i < msgs.length; i++) {
String message = msgs[i].getDisplayMessageBody();
if (message != null && message.length() > 0) {
String from = msgs[i].getOriginatingAddress();
if(FROM.contains("TM-MYAMEX")){
String body = message ;
Date datetime = new Date() ;
}
}
}
}
}
}
I'm not sure of what does "TM-MYAMEX" means but here is the code to get all SMS
.
我不确定“TM-MYAMEX”是什么意思,但这里是获取所有SMS
.
Date = new Date()
beacause its under a BroadcastReceiver
then the tme you get the message its the current time.
Date = new Date()
因为它在 a 下,BroadcastReceiver
那么您收到消息的时间是当前时间。
Hope this help.
希望这有帮助。