Android学习笔记二十二.使用ContentProvider实现数据共享(五).监听ContentProvider的数据改变
package com.example.android_content_4; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.app.AlertDialog; import android.content.ContentValues; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore.Images.Media; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.ListView; import android.widget.SimpleAdapter; public class MediaProviderTest extends Activity { private Button catBtn,addBtn; private ListView show; ArrayList<String> names=new ArrayList<String>(); ArrayList<String> descs=new ArrayList<String>(); ArrayList<String> fileNames=new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); catBtn=(Button)findViewById(R.id.cat); addBtn=(Button)findViewById(R.id.add); show=(ListView)findViewById(R.id.list); catBtn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { names.clear(); descs.clear(); fileNames.clear(); //1.通过ContentResolver查询所有图片信息 Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI,null,null,null,null); while(cursor.moveToNext()) { //a.获取图片的显示名 String name = cursor.getString(cursor.getColumnIndex(Media.DISPLAY_NAME)); //b.获取图片的详细描述 String desc = cursor.getString(cursor.getColumnIndex(Media.DESCRIPTION)); //c.获取图片的保存位置的数据 byte[] data = cursor.getBlob(cursor.getColumnIndex(Media.DATA)); //d.将图片名、图片描述、图片保存路径分别添加到names、descs、fileNames集合中 names.add(name); descs.add(desc); fileNames.add(new String(data,0,data.length-1)); } //2.创建一个List集合,List集合的元素是Map,并将names、descs两个集合对象的数据转换到Map集合中 List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>(); for(int i = 0; i<names.size() ;i++) { Map<String,Object> listItem = new HashMap<String,Object>(); listItem.put("name", names.get(i)); //获取列表中指定位置的元素,元素i的名称 listItem.put("desc", descs.get(i)); //元素i的属性 listItems.add(listItem); } //3.创建一个SimpleAdapter,并为show ListView组件设置Adapter SimpleAdapter simpleAdapter = new SimpleAdapter( MediaProviderTest.this,listItems ,R.layout.line ,new String[] {"name","desc"} ,new int[] {R.id.name,R.id.desc}); show.setAdapter(simpleAdapter); } }); //4.为show ListView的列表项单击事件添加监听器 show.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //a.加载View.xml界面布局代表的视图 View viewDialog=getLayoutInflater().inflate(R.layout.view, null); //b.获取viewDialog中Id为image的组件 ImageView image=(ImageView)viewDialog.findViewById(R.id.image); //c.设置image显示指定图片 image.setImageBitmap(BitmapFactory.decodeFile(fileNames.get(position))); //d.使用对话框显示用户单击的图片 new AlertDialog.Builder(MediaProviderTest.this) .setView(viewDialog).setPositiveButton("确定", null) .show(); } }); //5.为add按钮的单击事件绑定监听器 addBtn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { //a.创建ContentValues对象,准备插入数据 ContentValues values = new ContentValues(); values.put(Media.DISPLAY_NAME, "photo"); values.put(Media.DESCRIPTION, "示例图片"); values.put(Media.MIME_TYPE, "image/jpeg"); //b.插入数据,返回所插入数据对应的Uri Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values); //c.加载应用程序下photo图片 Bitmap bitmap=BitmapFactory.decodeResource(MediaProviderTest.this.getResources(),R.drawable.photo); OutputStream os = null; try{ os=getContentResolver().openOutputStream(uri); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os); os.close(); }catch(IOException e) { e.printStackTrace(); } } }); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <Button android:id="@+id/cat" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查看所有图片"/> <Button android:id="@+id/add" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="添加图片"/> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content"></ListView> </LinearLayout>
<?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" > <TextView android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/desc" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> (4)点击列表项,弹出对话框布局/res/layout/view.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" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
package com.example.android_content_5; import android.app.Activity; import android.database.ContentObserver; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; public class MonitorSms extends Activity { private TextView sms; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sms=(TextView)findViewById(R.id.message); //1.为content://sms的数据改变注册监听器 getContentResolver().registerContentObserver(Uri.parse("content://sms"),true, new SmsObserver(new Handler())); } //2.提供自定义的ContentObserver监听器类 private final class SmsObserver extends ContentObserver { //a.构造方法 public SmsObserver(Handler handler) { super(handler); } public void onChange(boolean selfChange) { //b.查询发送箱中的短信(处于正在发送状态的信息放在发送箱),即查询content://sms/outbox的全部数据 Cursor cursor = getContentResolver().query(Uri.parse("content://sms/outbox"), null, null, null, null); while(cursor.moveToNext()) { StringBuilder sb = new StringBuilder(); //获取短信的发送地址 sb.append("address=").append(cursor.getString(cursor.getColumnIndex("address"))); //获取短信的标题 sb.append(";subject=").append(cursor.getString(cursor.getColumnIndex("subject"))); //获取短信的内容 sb.append(";body=").append(cursor.getString(cursor.getColumnIndex("body"))); //获取短信的发送时间 sb.append(";time=").append(cursor.getLong(cursor.getColumnIndex("date"))); System.out.println("Has Sent SMS::"+sb.toString()); } } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。