編輯:關於Android編程
前兩篇博客並分別講了獲取聯系人和通話記錄的知識,這篇主要介紹短信獲取知識,短信在通訊管理中應該說是一個難點,因為短信涉及到短息會話和短信詳情兩個部分,並且短信的數據量比較大,可以采用AsyncQueryHandler框架來查詢,同時采用CursorAdapter來綁定數據
其中短信中可以來獲取聯系人的頭像和姓名。這個在代碼工具類中有實現,如果聯系人存在,則顯示姓名,否則顯示號碼,如果聯系人頭像存在則顯示頭像,否則顯示默認頭像,如圖片所示。這兩部分功能在聯系人和通話記錄中均可以實現,有興趣的童鞋可以修改。
查詢框架
package cn.zxw.contact.utils; import android.content.AsyncQueryHandler; import android.content.ContentResolver; import android.database.Cursor; import android.support.v4.widget.CursorAdapter; import android.util.Log; /** * QueryHandler框架 * @author zhan * */ public class QueryHandler extends AsyncQueryHandler { private static final String TAG = QueryHandler; public QueryHandler(ContentResolver cr) { super(cr); } @Override protected void onQueryComplete(int token, Object cookie, Cursor cursor) { super.onQueryComplete(token, cookie, cursor); String names[] = cursor.getColumnNames(); for (String name : names) { Log.i(TAG, name); } // 查詢完成 // if (cookie != null && cookie instanceof CursorAdapter) { // 把查詢出來的cursor結果設置給adapter CursorAdapter adapter = (CursorAdapter) cookie; adapter.changeCursor(cursor); } } }短信會話獲取
package cn.zxw.contact; import cn.zxw.contact.utils.ContactsUtils; import cn.zxw.contact.utils.QueryHandler; import cn.zxw.contact.utils.TimeUtils; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.support.v4.widget.CursorAdapter; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; /** * 短信列表 * 點擊短息列表中的條目會進入條目對應的短信詳情頁面 * @author zhan * */ public class MsgActivity extends Activity implements OnItemClickListener { private ListView lv; public MyCursorAdapter adapter; // 查詢的結果集 private String[] projection = new String[] { snippet, sms.thread_id as _id, msg_count, sms.address as address, sms.date as date }; private final static int SINPPET_COLUMN_INDEX = 0; private final static int THREAD_ID_COLUMN_INDEX = 1; private final static int MSG_COUNT_COLUMN_INDEX = 2; private final static int ADDRESS_COLUMN_INDEX = 3; private final static int DATE_COLUMN_INDEX = 4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_contacts_msg_calllog); lv = (ListView) findViewById(R.id.lv); adapter=new MyCursorAdapter(this, null);//初始化Adapter lv.setAdapter(adapter); startQuery(); lv.setOnItemClickListener(this); } /** * 執行查詢 */ private void startQuery() { QueryHandler mQueryHandler = new QueryHandler(getContentResolver()); // 執行查詢 /** * token唯一標識 cookie可以用來傳遞數據 上面的參數會傳遞給一個方法 onQueryComplete */ Uri uri = Uri.parse(content://sms/conversations); // mQueryHandler.startQuery(0, null, uri, projection, null, // null,date desc); mQueryHandler.startQuery(0, adapter, uri, projection, null, null, date desc); } // 自定義CursorAdapter private class MyCursorAdapter extends CursorAdapter { private LayoutInflater mInflater; public MyCursorAdapter(Context context, Cursor c) { super(context, c); mInflater = LayoutInflater.from(context); } // 創建條目 @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { View view = mInflater.inflate(R.layout.activity_msg_list_item, null); return view; } // 綁定條目 @Override public void bindView(View view, Context context, Cursor cursor) { // 1.獲取條目 // 2.獲取數據 // 3.綁定數據 ImageView iv_header = (ImageView) view.findViewById(R.id.iv_header); TextView tv_number = (TextView) view.findViewById(R.id.tv_number); TextView tv_body = (TextView) view.findViewById(R.id.tv_body); TextView tv_date = (TextView) view.findViewById(R.id.tv_date); // 由於Listview的數據顯示是通過適配器來控制的,所以到bindView方法中來控制checkbox 的顯示 // 獲取數據 int id = cursor.getInt(THREAD_ID_COLUMN_INDEX); String address = cursor.getString(ADDRESS_COLUMN_INDEX); int msg_count = cursor.getInt(MSG_COUNT_COLUMN_INDEX); long date = cursor.getLong(DATE_COLUMN_INDEX); String body = cursor.getString(SINPPET_COLUMN_INDEX); System.out.println(address); // 綁定數據 String displayName = ContactsUtils.getContactNameByAddress( getApplicationContext(), address); if (TextUtils.isEmpty(displayName)) { // 未知聯系人 iv_header.setImageResource(R.drawable.ic_launcher); tv_number.setText(address + ( + msg_count + )); } else { // 已知聯系人 tv_number.setText(displayName + ( + msg_count + )); Bitmap bitmap = ContactsUtils.getContactPhotoByAddress( getApplicationContext(), address); if (bitmap == null) { iv_header.setImageResource(R.drawable.ic_launcher); } else { iv_header.setImageBitmap(bitmap); } } tv_body.setText(body); String dataStr = TimeUtils .formatDate(getApplicationContext(), date); tv_date.setText(dataStr); } } @Override public void onItemClick(AdapterView parent, View view, int position, long id) { //傳遞數據 Cursor cursor = (Cursor) adapter.getItem(position); int _id = cursor.getInt(THREAD_ID_COLUMN_INDEX); String address=cursor.getString(ADDRESS_COLUMN_INDEX); String displayName=ContactsUtils.getContactNameByAddress(getApplicationContext(), address); //進入列表詳情頁面 Intent intent=new Intent(this, MsgDetailActivity.class); intent.putExtra(_id, _id); intent.putExtra(address, address); intent.putExtra(displayName, displayName); startActivity(intent); } }短信詳情頁面,可以實現發送短息
package cn.zxw.contact; import cn.zxw.contact.utils.Constants; import cn.zxw.contact.utils.ContactsMsgUtils; import cn.zxw.contact.utils.QueryHandler; import cn.zxw.contact.utils.TimeUtils; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.support.v4.widget.CursorAdapter; import android.text.TextUtils; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MsgDetailActivity extends Activity implements OnClickListener { private Button bt_back; private TextView tv_number; private ListView lv; private EditText et_msg_content; private Button bt_send; private int _id; private String address; private MyCursorAdapter adapter; private String[] projection=new String[]{ _id,type,body,date }; private final static int ID_COLUMN_INDEX=0; private final static int TYPE_COLUMN_INDEX=1; private final static int BODY_COLUMN_INDEX=2; private final static int DATE_COLUMN_INDEX=3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_msg_list_detail); initView(); startQuery(); } public void initView() { bt_back = (Button) findViewById(R.id.bt_back); tv_number = (TextView) findViewById(R.id.tv_number); lv = (ListView) findViewById(R.id.lv); et_msg_content = (EditText) findViewById(R.id.et_msg_content); bt_send = (Button) findViewById(R.id.bt_send); bt_back.setOnClickListener(this); bt_send.setOnClickListener(this); Intent intent=getIntent(); _id = intent.getIntExtra(_id, 0); address = intent.getStringExtra(address); String displayName=intent.getStringExtra(displayName); if (TextUtils.isEmpty(displayName)) { tv_number.setText(address); }else{ tv_number.setText(displayName); } adapter = new MyCursorAdapter(this, null); lv.setAdapter(adapter); } public void startQuery() { //查詢 QueryHandler mHandler=new QueryHandler(getContentResolver()); //查詢指定會話id裡面的所有短信 String selection=thread_id=?; String[] selectionArgs=new String[]{_id+}; mHandler.startQuery(0, adapter, Constants.SMS_URI, projection, selection, selectionArgs, date asc); } private class MyCursorAdapter extends CursorAdapter{ public MyCursorAdapter(Context context, Cursor c) { super(context, c); } @Override public View newView(Context context, Cursor cursor, ViewGroup viewGroup) { View view=getLayoutInflater().inflate(R.layout.activity_msg_detial_item, null); return view; } @Override public void bindView(View view, Context context, Cursor cursor) { TextView tv_date=(TextView) view.findViewById(R.id.tv_date); TextView tv_send=(TextView) view.findViewById(R.id.tv_send); TextView tv_receive=(TextView) view.findViewById(R.id.tv_receive); long date=cursor.getLong(DATE_COLUMN_INDEX); String body=cursor.getString(BODY_COLUMN_INDEX); int type=cursor.getInt(TYPE_COLUMN_INDEX); String str=TimeUtils.formatDate(getApplicationContext(), date); tv_date.setText(str); if (type==Constants.RECEIVE_TYPE) { //1表示接收 tv_date.setVisibility(View.VISIBLE); tv_receive.setText(body); tv_send.setVisibility(View.GONE); }else if (type==Constants.SEND_TYPE) { //2表示發送 tv_date.setVisibility(View.VISIBLE); tv_send.setVisibility(View.VISIBLE); tv_send.setText(body); tv_receive.setVisibility(View.GONE); } } /** * 該方法一定會被系統調用 */ @Override public void notifyDataSetChanged() { super.notifyDataSetChanged(); //讓Listview滾動到最後面 lv.setSelection(adapter.getCount()-1); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_back: finish();//關閉當前activity break; case R.id.bt_send: // 獲取短息內容 String content=et_msg_content.getText().toString().trim(); if (TextUtils.isEmpty(content)) { //發送內容為空 Toast.makeText(getApplicationContext(), 短信為空, 0).show(); }else{ ContactsMsgUtils.sendMsg(getApplicationContext(), address, content); //清空 et_msg_content.setText(); //短息發送成功後自動滾動到最後 } break; default: break; } } }短信發送方法:
/** * 發送短信 * @param address * @param content */ public static void sendMsg(Context context,String address,String content){ SmsManager smsManager=SmsManager.getDefault();//如果短信太長,拆分短信 ArrayListparts=smsManager.divideMessage(content); smsManager.sendMultipartTextMessage(address, null, parts, null, null); //手動存儲短信 ContentResolver resolver=context.getContentResolver(); Uri uri=Constants.SMS_URI; ContentValues values=new ContentValues(); values.put(address, address); values.put(body, content); values.put(type, Constants.SEND_TYPE); values.put(date, System.currentTimeMillis()); resolver.insert(uri, values); }
一、問題描述LBS位置服務是android應用中重要的功能,應用越來越廣泛,下面我們逐步學習和實現lbs相關的應用如定位、地圖、導航等,首先我們看如何基於百度地圖實現定位
在總結鎖屏代碼之前,有兩個中心思想要銘記於心 A) KeyguardHostView就是我們最終所要展示的界面,所以不論用什麼方法手段,都要將這個KeyguardHost
前面文章講解了Android的藍牙基本用法,本文講得深入些,探討下藍牙方面的隱藏API。用過Android系統設置(Setting)的人都知道藍牙搜索之後可以建立配對和解
由於第一次寫博客,裡面難免有些錯誤與不足,希望大家可以幫忙多多指點。下面我將從三個方面給大家介紹一下android開發吧,也是對我前一段時間學習的總結吧。第一,Andro