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.provider.ContactsContract;

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

public class Main {
    private static Pattern accountTypePattern = Pattern.compile("([a-zA-Z0-9]+)");

    public static String getAccountType(Context context, long id, String name) {
        try {
            Cursor cur = context.getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI,
                    new String[] { ContactsContract.RawContacts.ACCOUNT_TYPE,
                            ContactsContract.RawContacts.ACCOUNT_NAME },
                    ContactsContract.RawContacts.CONTACT_ID + " = ?", new String[] { String.valueOf(id) }, null);
            if (cur != null) {
                String str = "";
                while (cur.moveToNext()) {
                    str += cur.getString(cur.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE));
                }
                //                Log.v("getAccountType", name+" => "+str);
                cur.close();
                Matcher m = accountTypePattern.matcher(str);
                String last = "";
                while (m.find()) {
                    last = m.group(1);
                }
                return last;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}