java - Android 错误:Telephony.Threads 类型的 getOrCreateThreadId 方法未定义

标签 java android eclipse android-mms

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

在一个类方法中,有这样的代码:

threadId = Threads.getOrCreateThreadId(mContext, recipients);

mContext是上下文,recipients是Set

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.");
    }
}

为什么它没有找到该方法并告诉我它未定义?

谢谢!

最佳答案

你必须使用反射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;
}

这对我来说是工作

关于java - Android 错误:Telephony.Threads 类型的 getOrCreateThreadId 方法未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25874891/

相关文章:

eclipse - Eclipse Juno启动错误日志文件

java - java包名为什么要小写?

java - 如何分析 App Engine Java 上的实际内存使用情况?

android - 如何使用 ArcGIS SDK for Android 编辑离线 .geodatabase 文件?

android - 多个 RTSP 流式传输不起作用

java - Eclipse RCP : Determine mouse button that caused selection on SelectionChanged Event

java - 如果没有 wtpwebapps 文件夹,则无法在 Tomcat 7 上部署 WAR 文件

java - 通用列表中的唯一类

java - Android字符串日期到日期时间的转换

android - 在 Android SDK 文件夹中找不到 SDK Manager.exe