我可以使用TelephonyManager来获取用户的电话接线员,我需要使用此名称来检索从运营商处收到的文本。如何在Android的ContextResolver.query()方法中使用类似SQL where子句的内容?这是我的代码,用于检索用户收到的所有短信。我需要调整它以检索来自用户电话接线员的消息。
public void fetchSMS() {
TelephonyManager tMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String operator=tMgr.getNetworkOperatorName();
Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
String[] cols = new String[]{"_id", "thread_id", "address", "person", "date", "body", "type"};
Cursor cursor1 = getContentResolver().query(
mSmsinboxQueryUri,
cols, null, null, null);
if (cursor1!=null && cursor1.moveToFirst()) {
do {
String address = cursor1.getString(cursor1
.getColumnIndex(cols[2]));
//String address2=(address.split("-"))[1];
String date = cursor1.getString(cursor1
.getColumnIndex(cols[4]));
String msg = cursor1.getString(cursor1
.getColumnIndex(cols[5]));
String type = cursor1.getString(cursor1
.getColumnIndex(cols[6]));
smsLog.append("\n" + "Address:").append(address).append("\n" + "Date:").append(date).append("\n" + "Message:").append(msg).append("\n" + "Type:").append(type).append("\n\n");
cursor1.moveToNext();
}
while (!cursor1.isAfterLast());
cursor1.close();
}
}
答案 0 :(得分:1)
可能对你有用。
More Information refer this page
<强> SmsBroadcastReceiver.java 强>
这是一个BroadcastReceiver,它接收意图过滤的SMS消息。 onReceive,我们提取SMS消息包并显示Toast消息,并通过将SMS消息添加到SMS收件箱列表来更新UI。当短信到达时,收件箱会自动刷新。
public class SmsBroadcastReceiver extends BroadcastReceiver {
public static final String SMS_BUNDLE = "pdus";
public void onReceive(Context context, Intent intent) {
Bundle intentExtras = intent.getExtras();
if (intentExtras != null) {
Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
String smsMessageStr = "";
for (int i = 0; i < sms.length; ++i) {
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
String smsBody = smsMessage.getMessageBody().toString();
String address = smsMessage.getOriginatingAddress();
smsMessageStr += "SMS From: " + address + "\n";
smsMessageStr += smsBody + "\n";
}
Toast.makeText(context, smsMessageStr, Toast.LENGTH_SHORT).show();
//this will update the UI with message
SmsActivity inst = SmsActivity.instance();
inst.updateList(smsMessageStr);
}
}
}
<强> SmsActivity.java 强> 这是SMS应用程序的主要Android活动。它充当SMS收件箱。它有一个ListView来显示SMS消息。 onCreate我们从其Uri中读取内部SMS收件箱中的所有消息,并使用ListView显示它们。
public class SmsActivity extends Activity implements OnItemClickListener {
private static SmsActivity inst;
ArrayList<String> smsMessagesList = new ArrayList<String>();
ListView smsListView;
ArrayAdapter arrayAdapter;
public static SmsActivity instance() {
return inst;
}
@Override
public void onStart() {
super.onStart();
inst = this;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
smsListView = (ListView) findViewById(R.id.SMSList);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, smsMessagesList);
smsListView.setAdapter(arrayAdapter);
smsListView.setOnItemClickListener(this);
refreshSmsInbox();
}
public void refreshSmsInbox() {
ContentResolver contentResolver = getContentResolver();
Cursor smsInboxCursor = contentResolver.query(Uri.parse("content://sms/inbox"), null, null, null, null);
int indexBody = smsInboxCursor.getColumnIndex("body");
int indexAddress = smsInboxCursor.getColumnIndex("address");
if (indexBody < 0 || !smsInboxCursor.moveToFirst()) return;
arrayAdapter.clear();
do {
String str = "SMS From: " + smsInboxCursor.getString(indexAddress) +
"\n" + smsInboxCursor.getString(indexBody) + "\n";
arrayAdapter.add(str);
} while (smsInboxCursor.moveToNext());
}
public void updateList(final String smsMessage) {
arrayAdapter.insert(smsMessage, 0);
arrayAdapter.notifyDataSetChanged();
}
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
try {
String[] smsMessages = smsMessagesList.get(pos).split("\n");
String address = smsMessages[0];
String smsMessage = "";
for (int i = 1; i < smsMessages.length; ++i) {
smsMessage += smsMessages[i];
}
String smsMessageStr = address + "\n";
smsMessageStr += smsMessage;
Toast.makeText(this, smsMessageStr, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
}