0

2.1 プラットフォームを使用しています。エミュレータからすべての SMS を取得しようとしています。コードにコンパイル エラーLogcat エラーはありません。マニフェスト ファイルに ReadSMS のアクセス許可を含めました。コードを実行すると、ListView に表示されません。私の XML ファイルには、ListView が 1 つしかありません。その ListView で、エミュレーターから形式 (番号: Sms 本文) のすべての SMS を表示しようとしています。

コード

public class SMSActivity extends Activity {
/** Called when the activity is first created. */
    ListView lview;
    String Body  = "" ; 
    int Number;
    ArrayList<String> smslist=new ArrayList<String>();
    ArrayAdapter<String> itemAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);
    lview =(ListView)findViewById(R.id.lv);
    itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,smslist);
    lview.setAdapter(itemAdapter);

    ContentResolver cr = getContentResolver();
    Cursor c = cr.query(Uri.parse("content://sms/"), new String[] { "_id", "address", "date", "body","read" },"_id = "+null, null, null);

    while(c.moveToNext()){

    Number = c.getInt(c.getColumnIndexOrThrow("address"));

   Body = c.getString(c.getColumnIndexOrThrow("body")).toString();

   smslist.add( Number + "\n" + Body);
    }
    itemAdapter.notifyDataSetChanged();
    c.close();
}
} 

この問題を解決するには?

4

1 に答える 1

1

あなたがしているエラーは、あなたがSMSを読みたいと言っているだけですが、どのフォルダからそれらが必要かを言うのを忘れていました.eg inbox、outboxなど.

受信トレイから SMS を読む必要がある場合は、次を使用します。

Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);

を使用して列名をクエリすることもできます

for (int i = 0; i < cursor.getColumnCount(); i++) {

    Log.i("info", cursor.getColumnName(i));
}
于 2012-07-09T12:56:02.143 に答える