ContentResolver之短信读取
来源:互联网 发布:erp软件图标素材 编辑:程序博客网 时间:2024/05/29 13:19
代码:
package com.qianfeng.contenprovidersms;import java.text.SimpleDateFormat;import java.util.Date;import android.app.Activity;import android.content.ContentResolver;import android.content.Context;import android.database.Cursor;import android.net.Uri;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.CursorAdapter;import android.widget.ListView;import android.widget.TextView;/** * 获取系统短信的信息 * */public class MainActivity extends Activity {private ListView lv ;private ContentResolver resolver;//内容解析者 获取ContentProvider提供的数据private String uriSms ="content://sms";//短信的Uri @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);lv = (ListView) findViewById(R.id.lv);resolver = getContentResolver();//得到内容解析者对象/** * 参数1:Uri对象 * 参数2:查询数据表中字段数组 * 参数3: 查询的条件 * 参数4:查询条件的占位符 * 参数5: 排序 */Cursor cursor =resolver.query(Uri.parse(uriSms), null, null, null, null);MyCursorAdapter adapter = new MyCursorAdapter(MainActivity.this, cursor);lv.setAdapter(adapter);}/** * * 自定义的CursorAdapter * */public class MyCursorAdapter extends CursorAdapter{public MyCursorAdapter(Context context, Cursor c) {super(context, c);// TODO Auto-generated constructor stub}/** * 每个item的布局 */@Overridepublic View newView(Context context, Cursor cursor, ViewGroup parent) {// TODO Auto-generated method stubreturn LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);}/** * 将数据绑定到newView 返回的view的控件上 */@Overridepublic void bindView(View view, Context context, Cursor cursor) {// TODO Auto-generated method stubTextView tv_number = (TextView) view.findViewById(R.id.tv_number);TextView tv_time = (TextView) view.findViewById(R.id.tv_time);TextView tv_type = (TextView) view.findViewById(R.id.tv_type);//短信的号码String number = cursor.getString(cursor.getColumnIndex("address"));tv_number.setText(number);//发送短信的时间int date = cursor.getInt(cursor.getColumnIndex("date"));tv_time.setText(GetDate(date));int type = cursor.getInt(cursor.getColumnIndex("type"));String str =null ;if(1==type){//接受str ="接受";}else{//发送str ="发送";}tv_type.setText(str);}}/** * int 类型时间 转换成string * * @param date * @return */public String GetDate(int date) {SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");return format.format(new Date(date));}}
清单文件:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.qianfeng.contenprovidersms" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <!-- 获取消息的读写权限 --> <uses-permission android:name="android.permission.READ_SMS"/> <uses-permission android:name="android.permission.WRITE_SMS"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.qianfeng.contenprovidersms.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>
0 0
- ContentResolver之短信读取
- ContentResolver之读取联系人
- 使用ContentResolver读取通话记录与短信记录
- 读取联系人:ContentResolver
- ContentResolver读取音乐文件
- GSM之短信读取格式
- ContentResolver读取系统联系人数据
- 读写系统数据库中的短信:ContentResolver
- Android ContentProvider之读取和插入短信
- Android18_ContentProvider之ContentResolver
- Android19_ContentProvider之ContentResolver
- ContentProvider之ContentResolver
- 四大组件之ContentResolver
- 16、ContentProvider之ContentResolver
- 短信读取
- 读取短信
- 短信读取
- 读取短信
- 08-02 文件IO流 网络通信(TCP) 集合 泛型
- PyCharm安装Python插件
- UE4在VS2013中各个编译配置代表意义
- 使用WebRTC搭建前端视频聊天室-03——数据通道篇
- EXTJS-1 表单和ajax数据交换
- ContentResolver之短信读取
- session学习记录
- hdfs api
- hdu 4292 Food (最大流)
- 使用WebRTC搭建前端视频聊天室-04——点对点通信篇
- UE4制作插件的插件神器pluginCreator
- 分布式程序防止多用户对同一数据更新问题
- POJ 2253 Frogger (dijkstra)
- c++ primer Exercise 5.0