編輯:關於Android編程
ContentProvider管理聯系人的實例:
package com.android.xiong.getsystemcontentprovidertest; import java.util.ArrayList; import android.app.Activity; import android.app.AlertDialog; import android.content.ContentUris; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.provider.ContactsContract.CommonDataKinds.Email; import android.provider.ContactsContract.CommonDataKinds.Phone; import android.provider.ContactsContract.CommonDataKinds.StructuredName; import android.provider.ContactsContract.Data; import android.provider.ContactsContract.RawContacts; import android.view.Gravity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.AbsListView.LayoutParams; import android.widget.BaseExpandableListAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private Button bt1, bt2; private ExpandableListView exp1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt1 = (Button) findViewById(R.id.bt1); bt1.setOnClickListener(new LookPresonClick()); bt2 = (Button) findViewById(R.id.bt2); bt2.setOnClickListener(new AddPersonClick()); } class AddPersonClick implements OnClickListener { @Override public void onClick(View v) { // 獲取程序界面中的桑文本框 String name = ((EditText) findViewById(R.id.ed1)).getText() .toString(); String phone = ((EditText) findViewById(R.id.ed2)).getText() .toString(); String email = ((EditText) findViewById(R.id.ed3)).getText() .toString(); // 創建一個空的ContentValue ContentValues values = new ContentValues(); // 向RawContacts.CONTNT_URI執行一個空值插入 // 目的是獲取系統返回的rawContactId Uri rawContactsUri = getContentResolver().insert( RawContacts.CONTENT_URI, values); long rawContactId = ContentUris.parseId(rawContactsUri); values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); // 設置內容類型 values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE); // 設置聯系人名字 values.put(StructuredName.GIVEN_NAME, name); // 向聯系人Uri添加聯系人名字 getContentResolver().insert( android.provider.ContactsContract.Data.CONTENT_URI, values); values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); // 設置聯系人的電話 values.put(Phone.NUMBER, phone); // 設置電話類型 values.put(Phone.TYPE, Phone.TYPE_MOBILE); // 向聯系人電話Uri添加電話號碼 getContentResolver().insert( android.provider.ContactsContract.Data.CONTENT_URI, values); values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE); // 設置聯系人的email地址 values.put(Email.DATA, email); // 設置email的類型 values.put(Email.TYPE, Email.TYPE_WORK); getContentResolver().insert( android.provider.ContactsContract.Data.CONTENT_URI, values); Toast.makeText(MainActivity.this, "添加聯系人信息成功", Toast.LENGTH_LONG) .show(); } } class LookPresonClick implements OnClickListener { @Override public void onClick(View v) { // 定義兩個List來封裝系統聯系人信息,指定聯系人的電話,email等詳情 final ArrayList<String> names = new ArrayList<String>(); final ArrayList<ArrayList<String>> details = new ArrayList<ArrayList<String>>(); // 使用ContentResolver查找聯系人數據 Cursor cursor = getContentResolver().query( ContactsContract.Contacts.CONTENT_URI, null, null, null, null); // 遍歷結果 獲取系統所有聯系人信息 while (cursor.moveToNext()) { // 獲取聯系人ID String contactid = cursor.getString(cursor .getColumnIndex(ContactsContract.Contacts._ID)); // 獲取聯系人的名字 String name = cursor .getString(cursor .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); names.add(name); // 使用ContentResolver查找聯系人的電話號碼 Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "= ?", new String[] { contactid }, null); ArrayList<String> detail = new ArrayList<String>(); // 遍歷查詢結果,獲取該聯系人的多個電話 while (phones.moveToNext()) { // 獲取查詢的結果中的電話號碼列 String phoneNumber = phones .getString(phones .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); detail.add("電話號碼是:" + phoneNumber); } phones.close(); // 使用ContentResolver查找聯系人的E-mail地址 Cursor emails = getContentResolver().query( ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " =?", new String[] { contactid }, null); // 遍歷查詢結果,獲取該聯系人的多個email地址 while (emails.moveToNext()) { // 獲取查詢的結果中email地址中列的數據 String emailAddress = emails .getString(emails .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); detail.add("email是:" + emailAddress); } emails.close(); details.add(detail); } cursor.close(); // 加載result.xml界面布局代表的視圖 View resultDialog = getLayoutInflater().inflate(R.layout.result, null); exp1 = (ExpandableListView) resultDialog.findViewById(R.id.exp1); // 創建一個ExpandableListAdapter對象 ExpandableListAdapter adapter = new BaseExpandableListAdapter() { @Override public boolean isChildSelectable(int groupPosition, int childPosition) { // TODO Auto-generated method stub return true; } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return true; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { TextView text = getTextVeiw(); text.setText(getGroup(groupPosition).toString()); return text; } @Override public long getGroupId(int groupPosition) { // TODO Auto-generated method stub return groupPosition; } @Override public int getGroupCount() { // TODO Auto-generated method stub return names.size(); } @Override public Object getGroup(int groupPosition) { // TODO Auto-generated method stub return names.get(groupPosition); } @Override public int getChildrenCount(int groupPosition) { // TODO Auto-generated method stub return details.get(groupPosition).size(); } private TextView getTextVeiw() { AbsListView.LayoutParams lp = new LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 64); TextView textview = new TextView(MainActivity.this); textview.setLayoutParams(lp); textview.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); textview.setPadding(36, 0, 0, 0); textview.setTextSize(20); return textview; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView textview = getTextVeiw(); textview.setText(getChild(groupPosition, childPosition) .toString()); return textview; } @Override public long getChildId(int groupPosition, int childPosition) { // TODO Auto-generated method stub return childPosition; } @Override public Object getChild(int groupPosition, int childPosition) { return details.get(groupPosition).get(childPosition); } }; exp1.setAdapter(adapter); // 使用對話框來顯示查詢結果 new AlertDialog.Builder(MainActivity.this).setView(resultDialog) .setPositiveButton("確定", null).show(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <EditText android:id="@+id/ed1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="輸入聯系人姓名"/> <EditText android:id="@+id/ed2" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="輸入聯系人電話"/> <EditText android:id="@+id/ed3" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="輸入聯系人email"/> <Button android:id="@+id/bt2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="添加聯系人信息"/> <Button android:id="@+id/bt1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查看聯系人" /> </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" > <ExpandableListView android:id="@+id/exp1" android:layout_width="match_parent" android:layout_height="wrap_content" > </ExpandableListView> </LinearLayout>
使用ContentProvider管理多媒體內容
Android為多媒體提供的Uri:
1、MediaStore.Audio.Mdia.EXTERNAL_CONTENT_URI:存儲在外部設備上的音頻文件
2、MediaStore.Audio.Mdia.INTERNAL_CONTENT_URI:存儲在手機內部上的音頻文件
3、MediaStore.Images.Mdia.EXTERNAL_CONTENT_URI:存儲在外部設備上的圖片文件
4、MediaStore.Images.Mdia.INTERNAL_CONTENT_URI:存儲在內部設備上的圖片文件
5、MediaStore.Video.Mdia.EXTERNAL_CONTENT_URI:存儲在外部設備上的音頻文件
6、MediaStore.Video.Mdia.INTERNAL_CONTENT_URI:存儲在內部設備上的音頻文件
實例:
package com.example.mediaprovidertest; 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.Menu; 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; import android.widget.TextView; public class MainActivity extends Activity { private Button bt1, bt2; private ListView list1; 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.activity_main); bt1 = (Button) findViewById(R.id.bt1); bt2 = (Button) findViewById(R.id.bt2); list1 = (ListView) findViewById(R.id.list); bt1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 清空names、desc、fileName集合裡原有的數據 names.clear(); descs.clear(); filenames.clear(); // 通過ContentResolver查詢所有圖片信息 Cursor curos = getContentResolver().query( Media.EXTERNAL_CONTENT_URI, null, null, null, null); while (curos.moveToNext()) { // 獲取圖片顯示的名字 String name = curos.getString(curos .getColumnIndex(Media.DISPLAY_NAME)); // 獲取圖片的詳細信息、 String desc = curos.getString(curos .getColumnIndex(Media.DESCRIPTION)); // 將圖片名保存的位置數據 byte[] data = curos.getBlob(curos .getColumnIndex(Media.DATA)); // 將圖片名添加到names集合中 names.add(name); // 將圖片描述添加到desc集合中 descs.add(desc); // 將圖片保存路徑添加到fileNames集合中 filenames.add(new String(data, 0, data.length - 1)); } // 創建一個List集合的元素是map List<Map<String, Object>> listitems = new ArrayList<Map<String, Object>>(); // 將names、descs兩個集合對象的數據轉換到map集合 for (int i = 0; i < names.size(); i++) { Map<String, Object> listitem = new HashMap<String, Object>(); listitem.put("name", names.get(i)); listitem.put("desc", descs.get(i)); listitems.add(listitem); } SimpleAdapter simple = new SimpleAdapter(MainActivity.this, listitems, R.layout.items, new String[] { "name", "desc" }, new int[] { R.id.txt1, R.id.txt2 }); list1.setAdapter(simple); } }); list1.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // 加載view.xml界面布局代表視圖 View view = getLayoutInflater().inflate(R.layout.view, null); // 獲取viewDialog中ImageView組件 ImageView image1 = (ImageView) view.findViewById(R.id.image1); // 設置image顯示指定的圖片 image1.setImageBitmap(BitmapFactory.decodeFile(filenames .get(arg2))); // 使用對話框顯示用戶單擊的圖片 new AlertDialog.Builder(MainActivity.this).setView(view) .setPositiveButton("確定", null).show(); } }); bt2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 創建ContentValues對象,准備插入數據 ContentValues values = new ContentValues(); values.put(Media.DISPLAY_NAME, "jinta"); values.put(Media.DESCRIPTION, "金塔"); values.put(Media.MIME_TYPE, "image/jpeg"); // 插入數據對應的Uri Uri uri = getContentResolver().insert( Media.EXTERNAL_CONTENT_URI, values); // 加載應用程序下的jinta圖片 Bitmap bitmap = BitmapFactory.decodeResource( MainActivity.this.getResources(), R.drawable.jinta); OutputStream os = null; try { // 獲取剛插入的數據的Uri對應的輸出流 os = getContentResolver().openOutputStream(uri); // 將bitmap圖片保存到Uri對應的數據節點中 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os); os.close(); } catch (IOException io) { io.printStackTrace(); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <Button android:id="@+id/bt1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查看圖片"/> <Button android:id="@+id/bt2" 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/txt1" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/txt2" android:layout_width="match_parent" android:layout_height="wrap_content"/> </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" > <ImageView android:id="@+id/image1" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
本文實例為大家分享了Android發短信功能的實現方法,供大家參考,具體內容如下首先配置一個布局:<LinearLayout xmlns:android=http:
本文Github代碼鏈接https://github.com/AndroidMsky/AndoirdIOSPicker 先上圖吧:這是筆者最近一個項目一直再用的一個選擇器
SearchView是搜索框組件,它可以讓用戶在文本框裡輸入文字,通過監聽器取得用戶的輸入,當用戶點擊搜索時,監聽器執行實際的搜索。本文就為大家分享了SearchView
由於Android並未提供滑動開關之類的組件,所以我們需要自己去實現一個自定義的視圖組件來實現滑動開關效果。 這裡有一個示例代碼,它包括三個類:開關組件視圖、狀態監聽接口