Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.content.Context;

import android.database.Cursor;
import android.net.Uri;

public class Main {
    public static Long getOrCreateThreadId(Context context, String phone) {
        try {
            Uri threadIdUri = Uri.parse("content://mms-sms/threadID");
            Uri.Builder builder = threadIdUri.buildUpon();
            String[] recipients = { phone };
            for (String recipient : recipients) {
                builder.appendQueryParameter("recipient", recipient);
            }
            Uri uri = builder.build();
            Long threadId = 0L;
            Cursor cursor = context.getContentResolver().query(uri, new String[] { "_id" }, null, null, null);
            if (cursor != null) {
                try {
                    if (cursor.moveToFirst()) {
                        threadId = cursor.getLong(0);
                    }
                } finally {
                    cursor.close();
                }
                return threadId;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1L;
    }
}