android短信和彩信探秘threads

原创 2011年08月12日 19:35:57


android源代码在  彩信sms 表和彩信 pdu表,增加了一个触发器

CREATE TRIGGER delete_obsolete_threads_pdu AFTER 
DELETE ON pdu BEGIN   
DELETE FROM threads   
WHERE     _id = old.thread_id     AND _id NOT IN    
 (SELECT thread_id FROM sms      UNION SELECT thread_id from pdu);
 END

仔细看下就明白,如果threads表没有sms和pdu外部引用的时候,这条thread就会被删除。


thread被删除后,你再插入一条短信或者彩信(当然是代码插入),这时候因为没有thread id,所以就会不显示。


有人可能想到对threads表一起进行维护不就行了吗?  很不幸 ,系统对这个表的providers并不完全开放,只能用于查找。


但我们这时候又需要thread_id,   我对源代码进行了一些修改,把thread类提取了出来,仅供大家参考


package com.sweetop.provider;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.util.Patterns;

public final class Threads implements ThreadsColumns {
	private static final String[] ID_PROJECTION = { BaseColumns._ID };
	private static final String STANDARD_ENCODING = "UTF-8";
	private static final Uri THREAD_ID_CONTENT_URI = Uri
			.parse("content://mms-sms/threadID");
	public static final Uri CONTENT_URI = Uri.withAppendedPath(
			Uri.parse("content://mms-sms/"), "conversations");
	public static final Uri OBSOLETE_THREADS_URI = Uri.withAppendedPath(
			CONTENT_URI, "obsolete");
	public static final Pattern NAME_ADDR_EMAIL_PATTERN = Pattern
			.compile("\\s*(\"[^\"]*\"|[^<>\"]+)\\s*<([^<>]+)>\\s*");

	public static final int COMMON_THREAD = 0;
	public static final int BROADCAST_THREAD = 1;

	// No one should construct an instance of this class.
	private Threads() {
	}

	/**
	 * This is a single-recipient version of getOrCreateThreadId. It's
	 * convenient for use with SMS messages.
	 */
	public static long getOrCreateThreadId(Context context, String recipient) {
		Set<String> recipients = new HashSet<String>();

		recipients.add(recipient);
		return getOrCreateThreadId(context, recipients);
	}

	/**
	 * Given the recipients list and subject of an unsaved message, return its
	 * thread ID. If the message starts a new thread, allocate a new thread ID.
	 * Otherwise, use the appropriate existing thread ID.
	 * 
	 * Find the thread ID of the same set of recipients (in any order, without
	 * any additions). If one is found, return it. Otherwise, return a unique
	 * thread ID.
	 */
	public static long getOrCreateThreadId(Context context,
			Set<String> recipients) {
		Uri.Builder uriBuilder = THREAD_ID_CONTENT_URI.buildUpon();

		for (String recipient : recipients) {
			if (isEmailAddress(recipient)) {
				recipient = extractAddrSpec(recipient);
			}

			uriBuilder.appendQueryParameter("recipient", recipient);
		}

		Uri uri = uriBuilder.build();
		// if (DEBUG) Log.v(TAG, "getOrCreateThreadId uri: " + uri);

		Cursor cursor = context.getContentResolver().query(uri, ID_PROJECTION,
				null, null, null);
		if (true) {
			Log.v("Threads",
					"getOrCreateThreadId cursor cnt: " + cursor.getCount());
		}
		if (cursor != null) {
			try {
				if (cursor.moveToFirst()) {
					return cursor.getLong(0);
				} else {
					Log.e("Threads", "getOrCreateThreadId returned no rows!");
				}
			} finally {
				cursor.close();
			}
		}

		Log.e("Threads",
				"getOrCreateThreadId failed with uri " + uri.toString());
		throw new IllegalArgumentException(
				"Unable to find or allocate a thread ID.");
	}

	public static String extractAddrSpec(String address) {
		Matcher match = NAME_ADDR_EMAIL_PATTERN.matcher(address);

		if (match.matches()) {
			return match.group(2);
		}
		return address;
	}

	/**
	 * Returns true if the address is an email address
	 * 
	 * @param address
	 *            the input address to be tested
	 * @return true if address is an email address
	 */
	public static boolean isEmailAddress(String address) {
		if (TextUtils.isEmpty(address)) {
			return false;
		}

		String s = extractAddrSpec(address);
		Matcher match = Patterns.EMAIL_ADDRESS.matcher(s);
		return match.matches();
	}
}

当你需要获得一个thread id时,仅需调用

Threads.getOrCreateThreadId(this, address)


address是发送方的手机地址


如有问题,可以再评论中说明,我会一一回复。


另,转载注明出处


如果我的文章对您有帮助,请用支付宝打赏:



版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lastsweetop/article/details/6682876

Android项目实战——基于短信的聊天工具

综合前面的知识点,剔除繁杂的理论,注重实践,深入浅出综合使用Android中的知识实现一个短信管理类的App
  • 2017年04月28日 15:43

Android短彩信源码解析-短信发送流程(一)

有人问我怎么了解原生应用的流程,可能每个人有最适合每个人的方法,现将我的方法说一下,供大家参考: 1、通过DEBUG,DEBUG可以了解每一步代码做了什么,而且可以看到堆栈执行过程。 2、通过打Log...
  • hailushijie
  • hailushijie
  • 2013-08-22 21:27:29
  • 6205

关于Android中获取短信息会话(threads)表中的信息

欢迎使用Markdown编辑器写博客 本Markdown编辑器使用StackEdit修改而来,用它写博客,将会带来全新的体验哦: Markdown和扩展Markdown简洁的语法 代码块高亮 图...
  • tuying_001
  • tuying_001
  • 2017-04-24 23:58:39
  • 426

android短信和彩信探秘threads

短信和彩信的维护关键是Threads表,当遇到只剩一条短信或彩信时,对最后一条进行删除,这时候要注意android源代码在  彩信sms 表和彩信 pdu表,增加了一个触发器CREATE TRIGGE...
  • lastsweetop
  • lastsweetop
  • 2011-08-12 19:35:57
  • 41899

get linux thread id

first a thread program sample #include #include #include static int i = 0; void *threadfunc(...
  • elfylin
  • elfylin
  • 2011-12-17 18:03:18
  • 7604

android短信源码部分解析(双卡)

点击新建短信之后 @Override protected void onCreate(Bundle savedInstanceState) { mIsSmsEnabled ...
  • xunfan
  • xunfan
  • 2014-09-15 10:29:14
  • 1582

CreateThread

CreateThread是一种微软在Windows API中提供了建立新的线程的函数,该函数在主线程的基础上创建一个新线程。线程中止运行后,线程对象仍然在系统中,必须通过CloseHandle函数来关...
  • Qsir
  • Qsir
  • 2017-07-21 10:10:40
  • 145

Android短信列表源码分析

6572_message_conversationList_详细分析
  • xiashaohua
  • xiashaohua
  • 2014-10-28 20:58:15
  • 2757

短信发送的构造

这是我之前代码,没有存入到相应的发信箱中 PendingIntent pIntent = PendingIntent.getBroadcast(MmsWriteActivit...
  • jianguo_liao19840726
  • jianguo_liao19840726
  • 2012-04-11 17:04:25
  • 799

CreateThread()使用实例

一、函数解释 (参见MSDN) The CreateThread function creates a thread to execute within the virtual address s...
  • cuiyifang
  • cuiyifang
  • 2012-09-03 16:03:54
  • 2396
收藏助手
不良信息举报
您举报文章:android短信和彩信探秘threads
举报原因:
原因补充:

(最多只允许输入30个字)