Android--通过关键字查找短消息数据库并将匹配的信息显示_第1张图片

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>


更多相关文章

  1. [Android] 代码实现按钮/图片自旋转(中心旋转)
  2. android 的C++代码都加 namespace android
  3. Android代码混淆一定要Export Android Application,否则不起效
  4. Android的Location功能代码
  5. 第一行代码笔记之——Activity启动模式
  6. Android:简单联网获取网页代码
  7. Android 开发笔记 - Android Studio 代码模板
  8. android源代码下载及编译
  9. android 用代码画圆

随机推荐

  1. Android--Handler机制(一)(Android(安卓)
  2. Android(安卓)Intent匹配规则
  3. Android之Intent
  4. android 输入框 布局
  5. android使用webview加载flash文件
  6. Android(安卓)设计一个可单选,多选的Demo
  7. 2010.12.10(4)——— android MapView 处
  8. (转)Android软键盘弹出,界面整体上移
  9. Android(安卓)单元测试cmd 命令集
  10. Android: HowTo设置app不被系统kill掉