这是我的代码。它将允许我检索我从Android手机发送的所有消息。 我只想检索未读消息。
提前致谢。
private String load_sent_sms() {
// TODO Auto-generated method stub
Uri uriSms = Uri.parse("content://sms/sent");
Cursor cursor = getContentResolver().query(uriSms,
new String[] { "_id", "address", "date", "body" }, null, null,
null);
String sms = "";
cursor.moveToFirst();
while (cursor.moveToNext()) {
String address = cursor.getString(1);
String body = cursor.getString(3);
sms += "Contact Name: " + address + "\nMessage: " + body + "\n";
}
return sms;
}
答案 0 :(得分:4)
main.xml:
package com.example.esemesy;
import java.util.ArrayList;
import android.support.v7.app.ActionBarActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView lViewSMS = (ListView) findViewById(R.id.listViewSMS);
if(fetchInbox()!=null)
{
@SuppressWarnings("unchecked")
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, fetchInbox());
lViewSMS.setAdapter(adapter);
}
}
ArrayList fetchInbox (){
final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
//Retrieves all SMS (if you want only unread SMS, put "read = 0" for the 3rd parameter)
Cursor cursor = getContentResolver().query(SMS_INBOX, null, "read=0", null, null);
ArrayList sms = new ArrayList();
//Get all lines
while (cursor.moveToNext()) {
//Gets the SMS information
String address = cursor.getString(cursor.getColumnIndex("address"));
String person = cursor.getString(cursor.getColumnIndex("person"));
String date = cursor.getString(cursor.getColumnIndex("date"));
String protocol = cursor.getString(cursor.getColumnIndex("protocol"));
String read = cursor.getString(cursor.getColumnIndex("read"));
String status = cursor.getString(cursor.getColumnIndex("status"));
String type = cursor.getString(cursor.getColumnIndex("type"));
String subject = cursor.getString(cursor.getColumnIndex("subject"));
String body = cursor.getString(cursor.getColumnIndex("body"));
sms.add(address+"\n"+body);
//Do what you want
}
return sms;
}
}
mainlayout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.esemesy.MainActivity"
tools:ignore="MergeRootFrame" >
<ListView
android:id="@+id/listViewSMS"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</FrameLayout>
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.esemesy"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.READ_SMS">
</uses-permission>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.esemesy.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>