android2.x使用的通讯录数据库数据做了很多物理结构上的改动,经过翻墙查看官方文档之后受益良多,今天小议以下向通讯录插入联系人的方法
首先向RawContacts.CONTENT_URI执行一个空值插入,目的是获取系统返回的rawContactId 这时后面插入data表的依据,只有执行空值插入,才能使插入的联系人在通讯录里面可见,例如下面的代码
- ContentValues values = new ContentValues();
- Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
- long rawContactId = ContentUris.parseId(rawContactUri);
-
- values.clear();
- values.put(Data.RAW_CONTACT_ID, rawContactId);
- values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
- values.put(StructuredName.GIVEN_NAME, "Sullivan");
- values.put(StructuredName.FAMILY_NAME, "Mike");
- getContentResolver().insert(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, "13989297343");
- values.put(Phone.TYPE,Phone.TYPE_HOME);
- getContentResolver().insert(Data.CONTENT_URI, values);
按照这种传统的方法插入时每次只能插入元数据,就是某一种类型的一个值
- /**
- * 传统插入方法 每次只能插入元数据,就是某一种类型的一个值
- * 插入之后ContentValues需要被清空才能执行下一次插入数据操作
- */
- public void insert_traditional() {
- ContentValues values = new ContentValues();
- Uri rawContactUri = getContentResolver().insert(
- RawContacts.CONTENT_URI, values);
- long rawContactId = ContentUris.parseId(rawContactUri);
-
- values.clear();
- values.put(Data.RAW_CONTACT_ID, rawContactId);
- values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
- values.put(StructuredName.GIVEN_NAME, "Sullivan");
- values.put(StructuredName.FAMILY_NAME, "Mike");
- getContentResolver().insert(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, "13989297343");
- values.put(Phone.TYPE, Phone.TYPE_HOME);
- getContentResolver().insert(Data.CONTENT_URI, values);
-
- }
android官方同时介绍了另外一种新的批量插入的方法
- /**
- * 批量插入通讯数据 相对传统方法 更适合通讯录的导入操作
- * RawContacts.CONTENT_URI中 插入这个值
- * 是为了触发contacts表的某个函数 可以初始化和插入数据 使之能够在通讯看到
- * 插入StructuredName.DISPLAY_NAME 时格式为family name和given name中间 以空格隔开
- * 就不需要再插入family_name和given_name
- * 此方法相对传统方法来说在大量插入数据库比较节省时间
- * 批量插入方法需要一个库来支持Lists对象
- * 项目地址为http://code.google.com/p/google-collections/downloads/list
- * 我使用的是google-collect-1.0.jar
- */
- public void insert_batch() {
- try {
- ArrayList<contentprovideroperation> ops = Lists.newArrayList();
- int rawContactInsertIndex = ops.size();
- ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
- .withValue(RawContacts.DIRTY, "1")
- .build());
-
- ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
- .withValueBackReference(Data.RAW_CONTACT_ID,
- rawContactInsertIndex).withValue(Data.MIMETYPE,
- StructuredName.CONTENT_ITEM_TYPE).withValue(
- StructuredName.DISPLAY_NAME, "朱Sullivan")
- .build());
-
- ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
- .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
- .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
- .withValue(Phone.NUMBER, "13989297343")
- .withValue(Phone.TYPE, Phone.TYPE_HOME).build());
- getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
- } catch (RemoteException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (OperationApplicationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- </contentprovideroperation>
但是批处理方法有个不足之处就是,目前不支持MIMETYPE里面的NOTE,WEBSITE和NICKNAME的批量插入,这几个还需要用传统方法去操作
通过程序获取android系统手机的铃声和音量。同样,设置铃声和音量的方法也很简单!
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
//通话音量
int max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_VOICE_CALL );
int current = mAudioManager.getStreamVolume( AudioManager.STREAM_VOICE_CALL );
Log.d(“VIOCE_CALL”, “max : ” + max + ” current : ” + current);
//系统音量
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_SYSTEM );
current = mAudioManager.getStreamVolume( AudioManager.STREAM_SYSTEM );
Log.d(“SYSTEM”, “max : ” + max + ” current : ” + current);
//铃声音量
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_RING );
current = mAudioManager.getStreamVolume( AudioManager.STREAM_RING );
Log.d(“RING”, “max : ” + max + ” current : ” + current);
//音乐音量
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_MUSIC );
current = mAudioManager.getStreamVolume( AudioManager.STREAM_MUSIC );
Log.d(“MUSIC”, “max : ” + max + ” current : ” + current);
//提示声音音量
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_ALARM );
current = mAudioManager.getStreamVolume( AudioManager.STREAM_ALARM );
Log.d(“ALARM”, “max : ” + max + ” current : ” + current);
设置音量的方法也很简单,AudioManager提供了方法:
public void setStreamVolume(int streamType, int index, int flags)
其中 streamType 有内置的常量,去文档里面就可以看到
Windows Android Palm.Iphone Ophone
传感器类型分为:方向,加速度,光线。磁场,临近性,温度
准确性分为高,中,低,不可靠四种。不知道不可靠这种准确性是用来做什么的
还有采样率貌似是不确定的,依靠设备的本身的特性
作为与外界交互的一个窗口, 与传感器相关的接口,可以开发出很多有特色的,有趣的app出来。
不知道为何Feedsky在抽风还是怎么地? 导致我最近访问总是出问题,再烧录一个,原来的FeedSky继续有用。这一个Feed用FeedBurner,应该比较保险了吧?
废话少说,小二,上地址!
来勒~~~: http://feeds.feedburner.com/Allove_Blog
同时亦可以在右边的边栏找到订阅按钮,走你!