Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract.PhoneLookup;
import android.telephony.PhoneNumberUtils;

public class Main {
    public static String getPersonNameFromNumber(Context context, String box, String address) {
        if (address == null) {
            return "unknown";
        }
        if (!box.equalsIgnoreCase("draft")) {
            Cursor cursor = context.getContentResolver().query(
                    Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address)),
                    new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null);
            if (cursor != null) {
                try {
                    if (cursor.getCount() > 0) {
                        cursor.moveToFirst();
                        String name = cursor.getString(0);
                        return name;
                    }
                } finally {
                    cursor.close();
                }
            }
        }
        if (address != null) {
            return PhoneNumberUtils.formatNumber(address);
        }
        return "unknown";
    }
}