Java Android错误类型Telephony.Threads的方法getOrCreateThreadId未定义

Java Android错误类型Telephony.Threads的方法getOrCreateThreadId未定义,java,android,eclipse,android-mms,Java,Android,Eclipse,Android Mms,我正在Eclipse中构建一个Android应用程序 在一个类方法中,有以下代码: threadId = Threads.getOrCreateThreadId(mContext, recipients); mContext是一个上下文,并且设置了收件人 Eclipse在该行中显示了一个错误,即: 类型Telephony.Threads的方法getOrCreateThreadId(Context,Set)未定义 在文件Telephony.java中,有定义此方法的Threads类: /

我正在Eclipse中构建一个Android应用程序

在一个类方法中,有以下代码:

threadId = Threads.getOrCreateThreadId(mContext, recipients);
mContext是一个上下文,并且设置了收件人

Eclipse在该行中显示了一个错误,即:

类型Telephony.Threads的方法getOrCreateThreadId(Context,Set)未定义

在文件Telephony.java中,有定义此方法的Threads类:

    /**
 * Helper functions for the "threads" table used by MMS and SMS.
 */
public static 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(
            MmsSms.CONTENT_URI, "conversations");
    public static final Uri OBSOLETE_THREADS_URI = Uri.withAppendedPath(
            CONTENT_URI, "obsolete");

    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 (Mms.isEmailAddress(recipient)) {
                recipient = Mms.extractAddrSpec(recipient);
            }

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

        Uri uri = uriBuilder.build();

        Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(),
                uri, ID_PROJECTION, null, null, null);
        if (cursor != null) {
            try {
                if (cursor.moveToFirst()) {
                    return cursor.getLong(0);
                }
            } finally {
                cursor.close();
            }
        }

        throw new IllegalArgumentException("Unable to find or allocate a thread ID.");
    }
}
/**
*MMS和SMS使用的“线程”表的助手函数。
*/
公共静态最终类线程实现ThreadsColumns{
私有静态最终字符串[]ID_PROJECTION={BaseColumns.\u ID};
专用静态最终字符串标准_ENCODING=“UTF-8”;
私有静态最终Uri线程\u ID\u CONTENT\u Uri=Uri.parse(
"content://mms-sms/threadID");
公共静态最终Uri内容\u Uri=Uri.withAppendedPath(
MmsSms.CONTENT_URI,“对话”);
公共静态最终Uri过时\u线程\u Uri=Uri.withAppendedPath(
内容(“过时”);
公共静态final int COMMON_THREAD=0;
公共静态最终整数广播线程=1;
//任何人都不应该构造此类的实例。
私有线程(){
}
/**
*这是的单一收件人版本
*getOrCreateThreadId。它便于与SMS一起使用
*信息。
*/
公共静态长getOrCreateThreadId(上下文上下文,字符串收件人){
Set recipients=new HashSet();
收件人。添加(收件人);
返回getOrCreateThreadId(上下文,收件人);
}
/**
*给定未保存邮件的收件人列表和主题,
*返回其线程ID。如果消息启动新线程,
*分配一个新的线程ID。否则,请使用适当的
*现有线程ID。
*
*查找同一组收件人的线程ID(在
*任何订单,不添加任何内容)。如果有
*如果找到,则返回它。否则,返回唯一的线程ID。
*/
公共静态长getOrCreateThreadId(
上下文,设置收件人){
Uri.Builder-uriBuilder=THREAD\u ID\u CONTENT\u Uri.buildOn();
for(字符串收件人:收件人){
如果(彩信地址(收件人)){
收件人=Mms.extractAddrSpec(收件人);
}
uriBuilder.appendQueryParameter(“收件人”,收件人);
}
Uri=uriBuilder.build();
Cursor Cursor=SqliteWrapper.query(上下文,context.getContentResolver(),
uri,ID_投影,null,null,null);
如果(光标!=null){
试一试{
if(cursor.moveToFirst()){
返回cursor.getLong(0);
}
}最后{
cursor.close();
}
}
抛出新的IllegalArgumentException(“无法找到或分配线程ID”);
}
}
为什么它找不到方法并告诉我它是未定义的


谢谢

您必须使用反射java,因此您需要搜索此方法并像这样调用它:

public long getOrCreateThreadId(Context context,
        ArrayList<String> recipientsAddress) {

    Set<String> recipients = new HashSet<String>(recipientsAddress);
    Class[] paramTypes = new Class[2];
    paramTypes[0] = Context.class;
    paramTypes[1] = Set.class;

    Method getOrCreateThreadId=null;
    Class classToInvestigate=null;
    Object cC=null;
    try {
        String className = "android.provider.Telephony";

        classToInvestigate = Class.forName(className);
        //cC = classToInvestigate.newInstance();
        Class[] classes = classToInvestigate.getClasses();

        Toast.makeText(context, "*****Classes:  "+classes.length, Toast.LENGTH_LONG).show();

        Class threadsClass = null;
        for(Class c:classes){
            if(c.equals((Class)Telephony.Threads.class))
                threadsClass = c;
        }


        getOrCreateThreadId = threadsClass.getMethod("getOrCreateThreadId", paramTypes);
        getOrCreateThreadId.setAccessible(true);
        Log.e("Methode", "" + getOrCreateThreadId.toString());
        //Toast.makeText(getApplicationContext(), getOrCreateThreadId.toString(), Toast.LENGTH_LONG).show();

        Object arglist[] = new Object[2];
        arglist[0] = context;
        arglist[1] = recipients; // Not a real phone number

        Long threadId=(Long)getOrCreateThreadId.invoke(null,arglist);
        Toast.makeText(context, "*****ThreadId"+threadId.longValue(), Toast.LENGTH_LONG).show();
        return threadId;

    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        Toast.makeText(context, "NoSuchMethodException "+e.getMessage(), Toast.LENGTH_LONG).show();
        e.printStackTrace();
    } catch (ClassNotFoundException e1) {
        // TODO Auto-generated catch block
        Toast.makeText(context, "ClassNotFoundException "+e1.getMessage(), Toast.LENGTH_LONG).show();
        e1.printStackTrace();
    }catch (IllegalAccessException e3) {
        // TODO Auto-generated catch block
        Toast.makeText(context, "IllegalAccessException "+e3.getMessage(), Toast.LENGTH_LONG).show();
        e3.printStackTrace();
    } catch (InvocationTargetException e4) {
        Toast.makeText(context, "InvocationTargetException "+e4.getMessage(), Toast.LENGTH_LONG).show();
        e4.printStackTrace();
    }
    return -1;
}
public long getOrCreateThreadId(上下文、,
ArrayList收件人(Saddress){
Set recipients=新哈希集(recipientsAddress);
Class[]paramTypes=新类[2];
paramTypes[0]=Context.class;
paramTypes[1]=Set.class;
方法getOrCreateThreadId=null;
类classToInvestigate=null;
对象cC=null;
试一试{
String className=“android.provider.Telephony”;
classToInvestigate=Class.forName(className);
//cC=classToInvestigate.newInstance();
Class[]classes=classToInvestigate.getClasses();
Toast.makeText(上下文,****Classes:+Classes.length,Toast.length_LONG.show();
类threadsClass=null;
用于(c类:类){
if(c.equals((类)Telephony.Threads.Class))
螺纹等级=c;
}
getOrCreateThreadId=threadsClass.getMethod(“getOrCreateThreadId”,参数类型);
getOrCreateThreadId.setAccessible(true);
Log.e(“Methode”,“getOrCreateThreadId.toString());
//Toast.makeText(getApplicationContext(),getOrCreateThreadId.toString(),Toast.LENGTH_LONG).show();
对象arglist[]=新对象[2];
arglist[0]=上下文;
arglist[1]=收件人;//不是真实的电话号码
Long threadId=(Long)getOrCreateThreadId.invoke(null,arglist);
Toast.makeText(上下文,“******ThreadId”+ThreadId.longValue(),Toast.LENGTH_LONG.show();
返回threadId;
}捕获(无此方法例外){
//TODO自动生成的捕捉块
Toast.makeText(上下文,“NoSuchMethodException”+e.getMessage(),Toast.LENGTH\u LONG.show();
e、 printStackTrace();
}捕获(ClassNotFoundException e1){
//TODO自动生成的捕捉块
Toast.makeText(上下文,“ClassNotFoundException”+e1.getMessage(),Toast.LENGTH_LONG.show();
e1.printStackTrace();
}捕获(非法访问例外e3){
//TODO自动生成的捕捉块
Toast.makeText(上下文,“IllegalAccessException”+e3.getMessage(),Toast.LENGTH_LONG.show();
e3.printStackTrace();
}捕获(调用TargetException e4){
Toast.makeText(上下文,“InvocationTargetException”+e4.getMessage(),Toast.LENGTH_LONG.show();
e4.printStackTrace();
}
返回-1;
}

这对我来说很有用

下面是上述反射代码的一个简单版本:

        private static long getOrCreateThreadId(Context context, Set<String> recipientsAddress) {
            try {
                Class<?> clazz = Class.forName("android.provider.Telephony$Threads");
                Method method = clazz.getMethod("getOrCreateThreadId", Context.class, Set.class);
                method.setAccessible(true);
                return (Long) method.invoke(null, context, recipientsAddress);
            } catch (Exception e) {
                throw new IllegalArgumentException("Unable to find or allocate a thread ID.", e);
            }
        }
private静态长getOrCreateThreadId(上下文上下文,设置接收者){
试一试{
Class clazz=Class.forName(“android.provider.Telephony$Threads”);
方法Method=clazz.getMethod(“getOrCreateThreadId”,Context.class,Set.class);
方法setAccessible(true);
return(Long)method.invoke(null、context、recipientsAddress);
}抓住(