黑莓6 联系人备份与恢复

所谓的备份就是把联系人取出按照vcard的方式保存。

首先将联系人序列化

	public boolean createVCard() throws PIMException,
			UnsupportedEncodingException {
		ContactList contactList = (ContactList) PIM.getInstance().openPIMList(
				PIM.CONTACT_LIST, PIM.READ_ONLY);
		String[] dataFormats = PIM.getInstance().supportedSerialFormats(
				PIM.CONTACT_LIST);
		// byteStream = new ByteArrayOutputStream();
		mVCard = new Vector();
		Enumeration e = contactList.items();
		while (e.hasMoreElements()) {
			Contact c = (Contact) e.nextElement();
			byteStream = new ByteArrayOutputStream();
			PIM.getInstance().toSerialFormat(c, byteStream, "UTF8",
					dataFormats[0]);
			mVCard.addElement(byteStream);
		}
		writeFile(mVCard);

		return e.hasMoreElements();
		// return byteStream;
	}
然后将联系人流写入文件
	public boolean writeFile(Vector data) {
		FileConnection oldCon = null;
		OutputStream out = null;
		ByteArrayOutputStream bt = null;
		// InputStream input = null;
		try {
			oldCon = (FileConnection) Connector
					.open("file:///SDCard/Contacts.vcf");
			if (oldCon.exists()) {
				oldCon.delete();
			}
			if (!oldCon.exists()) {
				oldCon.create();
			}
			out = oldCon.openOutputStream();
			for (int i = 0; i < data.size(); i++) {
				bt = (ByteArrayOutputStream) data.elementAt(i);
				out.write(bt.toByteArray());
			}
			out.flush();
		} catch (IOException e) {
			Log.trace("Exception == " + e.getMessage());
			e.printStackTrace();
			return false;
		} finally {
			try {
				if (oldCon != null) {
					oldCon.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (Exception e) {
				Log.trace("Exception 1 == " + e.getMessage());
				e.printStackTrace();
				return false;
			}
		}
		return true;
	}
这样联系人就保存成像android一样的vcard名片格式。可以用文本方式打开看一下。


解析方面由于黑莓相关资料太少,没有找到比较优秀的解析方法,而且黑莓的写入联系人方式和andorid也不一样,所以自己写了一个直接截取字符串的方法,直接截取整条联系人信息。

	public boolean readFile(String path) {
		FileConnection oldCon = null;
		OutputStream out = null;
		InputStream input = null;
		ByteArrayOutputStream baos = null;
		try {
			oldCon = (FileConnection) Connector.open(path);
			if (!oldCon.exists()) {
				oldCon.create();
			}
			input = oldCon.openInputStream();
			baos = new ByteArrayOutputStream();

			byte buf[] = new byte[1024 * 8];
			int len = -1;
			while ((len = input.read(buf)) != -1) {
				baos.write(buf, 0, len);
			}
			String str = baos.toString().trim();
			// Log.trace("read str="+str);
			readFileVector = new Vector();
			int count = 0;
			try {
				while (getNextContact(str.trim())) {
					str = getContact(str.trim(), readFileVector);
					count++;
				}
				Log.trace("count=" + count);
			} catch (Exception e) {
				Log.trace("e=" + e);
				Log.trace("count=" + count);
			}
		} catch (Exception e) {
			Log.trace("Exception == " + e.getMessage());
			e.printStackTrace();
			return false;
		} finally {
			try {
				if (oldCon != null) {
					oldCon.close();
				}
				if (out != null) {
					out.close();
				}
				if (input != null) {
					input.close();
				}
				if (baos != null) {
					baos.close();
				}
			} catch (IOException e) {
				Log.trace("Exception 1 == " + e.getMessage());
				e.printStackTrace();
				return false;
			}
		}
		return true;
	}

判断继续截取方法

	private boolean getNextContact(String str) {
		String one = str.substring(str.indexOf("BEGIN:VCARD"), str
				.indexOf("END:VCARD") + 9);
		// Log.trace("one=="+one);
		String cutone = str.substring(one.length());
		if ("".equals(cutone) || null == cutone) {
			return false;
		} else {
			return true;
		}
	}
截取字符串方法
	private String getContact(String str, Vector v) {
		String one = str.substring(str.indexOf("BEGIN:VCARD"), str
				.indexOf("END:VCARD") + 9);
		v.addElement(one);
		String cutone = str.substring(one.length() + 1);
		return cutone;
	}

将读取的每一条数据写入联系人之前首先要删除所有联系人

	public void deleteAllContacts() throws UnsupportedEncodingException,
			PIMException {
		ContactList contactList = (ContactList) PIM.getInstance().openPIMList(
				PIM.CONTACT_LIST, PIM.READ_WRITE);
		Enumeration e = contactList.items();
		while (e.hasMoreElements()) {
			Contact c = (Contact) e.nextElement();
			contactList.removeContact(c);
		}
	}

然后将联系人写入

	public void readVCard(Vector v) throws UnsupportedEncodingException,
			PIMException {
		deleteAllContacts();
		// Import contact from vCard.
		for (int i = 0; i < v.size(); i++) {
			ByteArrayInputStream is = new ByteArrayInputStream(((String) v
					.elementAt(i)).getBytes());
			PIMItem[] pi = PIM.getInstance().fromSerialFormat(is, "UTF8");
			ContactList contactList = (ContactList) PIM.getInstance()
					.openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE);
			Contact contact2 = contactList.importContact((Contact) pi[0]);
			contact2.commit();
		}
	}

OVER。。。。估计没人看。。写下来记录自己的劳动。。。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值