近段时间做短信应用的相关开发,从最初的基本功能完成,到现在的操作性流畅,一步步优化,使运行速度变快、操作方便。从这一步步的调试过程中学习了不少知识,印象最为深刻的就是关于Uri的选择。 查询彩信和短信数据中每一个号码最新的一条信息:
Uri uri = Uri.parse(“content://mms-sms/conversations?simple=true”);
Cursor cursor = cr.query(uri, null, null, null, “date DESC”);
while (cursor.moveToNext()) {
cursor.getString(cursor.getColumnIndex(“_id”));//thread_id cursor.getLong(cursor.getColumnIndex(“date”));//发送或接收时间 cursor.getInt(cursor.getColumnIndex(“message_count”));//当前对话的所以信息总数
cursor.getString(cursor.getColumnIndex(“recipient_ids”));//canonical_address表的_id
cursor.getInt(cursor.getColumnIndex(“read”));//是否已读,0未读,1已读 cursor.getInt(cursor.getColumnIndex(“snippet_cs”));//标识此条数据是彩信还是短信,为空或是值为106为彩信,其它为短信 cursor.getString(cursor.getColumnIndex(“snippet”));//短信body,或是彩信主题 } cursor.close();
如图 通过thread_id查询与某联系人的所有聊天记录:
Uri uri = Uri.parse(“content://mms-sms/conversations/”+threadID); android 2.x的版本,联系人姓名、联系人电话号码 ,以及photo都分别存在不同的表中,第一次获取这些信息的时候,费了相当大的功夫,是一个表一个表的去匹配查询,不仅工作量大,而且速度极慢,后来才发现,系统提供了一个Uri可以一次把所有相关信息查询出来: android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 关于优化,最重要的就是查询出来的优化,只要查询速度快了,每个activity打开的速度自然也就快了,希望所有的童鞋们以后不要像我一样走弯路,费时费力,效率又低,找到合适的Uri,应用程序的效率才会高。
重要方法例子
AnimationSet as = new AnimationSet(true); AlphaAnimation als = new AlphaAnimation(1, 0); als.setDuration(1000); //设置为true, 动画执行后, 控件停留在执行结束后的状态 als.setFillAfter(true); //设置为true, 动画执行后, 控件返回执行前得状态 als.setFillBefore(false); //设置1000ms, 表示1000ms后再执行动画 als.setStartOffset(1000); //设置执行动画5次 als.setRepeatCount(5); as.addAnimation(als); iv.startAnimation(as);
完整例子:
package com.cn; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.ImageView; public class AnimationActivity extends Activity { /** Called when the activity is first created. */ ImageView iv = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); iv = (ImageView)findViewById(R.id.imageView1); } public void translate(View v){ AnimationSet as = new AnimationSet(true); /** * 参数: * 1. X轴坐标类型 * 2. X轴开始位置 * 3. X轴坐标类型 * 4. X轴移动后坐标位置. 0.5f为X轴的一半 * 5. Y轴坐标类型 * 6. Y轴开始位置 * 7. Y轴坐标类型 * 8. Y轴移动后坐标位置. 1f为Y轴底部 */ TranslateAnimation als = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1.0f ); als.setDuration(1000); as.addAnimation(als); iv.startAnimation(as); } public void alpha(View v){ AnimationSet as = new AnimationSet(true); AlphaAnimation als = new AlphaAnimation(1, 0); als.setDuration(1000); //设置为true, 动画执行后, 控件停留在执行结束后的状态 als.setFillAfter(true); //设置为true, 动画执行后, 控件返回执行前得状态 als.setFillBefore(false); //设置1000ms, 表示1000ms后再执行动画 als.setStartOffset(1000); //设置执行动画5次 als.setRepeatCount(5); as.addAnimation(als); iv.startAnimation(as); } public void rotate(View v){ AnimationSet as = new AnimationSet(true); /** * 参数: * 1. 旋转开始角度 * 2. 旋转结束角度 * 3. X轴坐标类型. Animation.RELATIVE_TO_PARENT相对于父控件的坐标. * Animation.RELATIVE_TO_SELF相对于自身的坐标 * 4. X轴的坐标比例, 1f表示X轴末端 * 5. Y轴坐标类型 * 6. Y轴坐标比例 */ RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_PARENT, 1f, Animation.RELATIVE_TO_PARENT, 0f); ra.setDuration(1000); as.addAnimation(ra); iv.startAnimation(as); } public void scale(View v){ AnimationSet as = new AnimationSet(true); /** * 参数: * 1. X轴开始比例 * 2. X轴结束比例 * 3. Y轴开始比例 * 4. Y轴结束比例 * 5. X轴坐标类型. Animation.RELATIVE_TO_PARENT相对于父控件的坐标. * Animation.RELATIVE_TO_SELF相对于自身的坐标 * 6. X轴缩小或者放大最后中心点 * 7. Y轴坐标类型. Animation.RELATIVE_TO_PARENT相对于父控件的坐标. * Animation.RELATIVE_TO_SELF相对于自身的坐标 * 8. Y轴缩小或者放大最后中心点 */ ScaleAnimation als = new ScaleAnimation(1, .1f, 1, .1f, Animation.RELATIVE_TO_SELF,.5f, Animation.RELATIVE_TO_SELF,.5f); als.setDuration(1000); as.addAnimation(als); iv.startAnimation(as); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="wrap_content" android:layout_weight="1"> <ImageView android:layout_height="wrap_content" android:id="@+id/imageView1" android:layout_width="wrap_content" android:src="/blog_article/@drawable/icon/index.html" ></ImageView> </LinearLayout> <LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:layout_width="match_parent" android:orientation="vertical"> <Button android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/button1" android:text="@string/translate" android:onClick="translate"></Button> <Button android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/button3" android:text="@string/alpha" android:onClick="alpha"></Button> <Button android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/button2" android:text="@string/rotate" android:onClick="rotate"></Button> <Button android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/button4" android:text="@string/scale" android:onClick="scale"></Button> </LinearLayout> </LinearLayout>
在这里我只说在项目中常用的一种添加NVRAM方式。
(1)在Nvram_user_defs.h中定义LID NVRAM_EF_EDUCATION_LID
NVRAM 中的数据是在 fs 文件系统的管理中操作的,而文 件操作需要识别数据项和管理数据项,都是通过这个 LID 来操作的。
(2)在Nvram_user_defs.h中定义size和total,
NVRAM_EDUCATION_DATA_SIZE 4:相当于一个内存块的大小
NVRAM_EDUCATION_DATA_TOTAL 1:一共有几个内存快
(3)定义NVRAM项Version 在Custom_nvram_editor_data_item.h中定义 #define NVRAM_EF_EDUCATION_LID_VERNO "001"
文件系统是怎么知道 它所管理的这些 NV 数据已经改变了呢?就是通过这个版本号来识别的,比如你修改了某个 NV 数据但没有更该它的版本号,文件 系统是不会对原始数据更改的;只有在更改了 NV 值并且修改了版本号,这时文件系统才识别到已经更改,并进行相应的数据更新 。
(4)定义NVRAM默认值使用默认的可以不用定义
(5)
在Nvram_user_config.c中使用
{
NVRAM_EF_EDUCATION_LID
,
NVRAM_EDUCATION_DATA_SIZE,
NVRAM_EDUCATION_DATA_TOTAL,
NVRAM_NORMAL(NVRAM_EF_ZERO_DEFAULT),这里就使用系统内的默认值
NVRAM_ATTR_MULTIREC_READ,
NVRAM_CATEGORY_USER,
"CT05",这里要注意这个序号,跟着上面那个来,上面那个如果是CT04这里就写CT05,这可不要认为是自己随便写
VER(NVRAM_EF_EDUCATION_LID),
"Education\0",这个可以随便写
NVRAM_RESERVED_VALUE
}
最主要说的就是我们使用的读写方法,有两个WriteValue/ReadValue或是WriteRecord/ReadRecord。但是这里只能使用WriteRecord/ReadRecord,如果使用WriteValue/ReadValue就会出现复位现象。而且要注意在使用的时候这里的大小不能使用DS_BYTE /DS_SHORT/DS_DOUBLE,要用我们上面定义的 NVRAM_EDUCATION_DATA_SIZE。
ReadRecord(
NVRAM_EF_EDUCATION_LID
,
1,//表示你使用到那一块内存
(&read_buff,
NVRAM_EDUCATION_DATA_SIZE,//注意这里不能使用DS_BYTE /DS_SHORT/DS_DOUBLE这三个
。
&pError);
WriteRecord使用方法一样。
要想使用WriteValue/ReadValue可以,修改两个文件custom_mmi_default_value.h和Common_mmi_cache_config.c中
在custom_mmi_default_value.h中定义LID NVRAM_MY_EDUCATION_UN, 注意在这个文件中有有三个枚举变量,分别是BYTEDATA,SHORTDATA,DOUBLEDATA,每一个枚举变量都对应在Mmi_frm_nvram_gprot.h中的
typedef enum
{
/* Corresponding to NVRAM_EF_CACHE_BYTE_LID. */
DS_BYTE = 1,
/* Corresponding to NVRAM_EF_CACHE_SHORT_LID. */
DS_SHORT = 2,
/* Corresponding to NVRAM_EF_CACHE_DOUBLE_LID. */
DS_DOUBLE = 8
} DATASIZE;
在 Common_mmi_cache_config.c文件中定义
SHORT_ENTRY(NVRAM_MY_EDUCATION_UN
,
NVRAM_APP_PHNSET,
NVRAM_CACHE_ID_RESERVED,
#ifdef _SETTING
NVRAM_COMMON_AVERAGE |NVRAM_COMMON_FACTORY_RESTORE|NVRAM_COMMON_USER_RESTORE ,
#else
KAL_TRUE,
#endif
"EDUCATIONLID",
0xFF,0xFF),
注意在这个文件中有三个数组,分别是common_mmi_cache_byte[],common_mmi_cache_short[] ,common_mmi_cache_double[]和上面LID定义中的BYTEDATA,SHORTDATA,DOUBLEDATA想对应起来
所以这里别忘了放错地方了。
定义好之后就可以使用WriteValue/ReadValue函数了,而且第三个参数可以相应的是DS_BYTE ,DS_SHORT ,DS_DOUBLE ,这里要注意之前LID定义在哪里就要相应的使用哪个,不要说在custom_mmi_default_value.h文件中定义的LID是在这个SHORTDATA枚举类型中的,那么在使用WriteValue/ReadValue函数时第三个参数就要使用DS_SHORT,不然会出现复位现象。