日期:2014-05-16 浏览次数:20480 次
java代码:
package com.example.test0123; import android.app.Activity; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.SimpleCursorAdapter; /** * 通过输入关键字在短信息数据库中查找有关键字的短消息 * 需要读短消息的权限<uses-permission android:name="android.permission.READ_SMS" /> * @author ZLQ * */ public class TestSmsSearch extends Activity { Button bu;//查询按钮 EditText et;//输入框 ListView lv;//用于显示查询结果的列表 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sms); bu = (Button) findViewById(R.id.button1); et = (EditText) findViewById(R.id.editText1); lv = (ListView) findViewById(R.id.listView1); bu.setOnClickListener(new OnClickListener() { public void onClick(View v) { String key = et.getText().toString(); //查询sms数据库的address和body两列,即信息发送者和信息内容,使用SimpleCursorAdapter必须要有_id列 Cursor c = getContentResolver().query( Uri.parse("content://sms"), new String[] { "_id", "address", "body" }, //通过where body like %key%进行模糊查询 "body like ?", new String[] { "%" + key + "%" }, null); //SimpleCursorAdapter适配器 SimpleCursorAdapter sim = new SimpleCursorAdapter(TestSmsSearch.this, android.R.layout.simple_list_item_2, c, new String[] { "address", "body" }, new int[] { android.R.id.text1, android.R.id.text2 }); lv.setAdapter(sim); } }); } }
sms.xml代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>