retrieve mms address number - android

In my application I'm able to get all MMS through code below, but I am not able to get the mms address. Can you guys/girls please help me?
Cursor cursor = activity.getContentResolver().query(Uri.parse("content://mms"),null,null,null,date DESC);
count = cursor.getCount();
if (count > 0) {
cursor.moveToFirst();
long messageId = cursor.getLong(0);
long threadId = cursor.getLong(1);
long timestamp = cursor.getLong(2);
String subject = cursor.getString(3);
}

to get address from MMS. do something like this
in msgnumber pass your messageID.
String add="";
final String[] projection = new String[] { "address", "contact_id", "charset", "type" };
final String selection = "type=137"; // "type="+ PduHeaders.FROM,
Uri.Builder builder = Uri.parse("content://mms").buildUpon();
builder.appendPath(String.valueOf(msgnumber)).appendPath("addr");
Cursor cursor = context.getContentResolver().query(
builder.build(),
projection,
selection,
null, null);
if (cursor.moveToFirst()) {
add = cursor.getString(0);
}
Hope this will help.

Related

How to search for contacts in android with partial phone number

I am trying to do fuzzy search of contacts in my android application with partial phone number, with below code. But my search function always ends in no result.
For example,
I have contact with phone number 1234567890 with name as "example".
fuzzySearch("4567"); should have return with contact named "example".
Can some please point me where am I wrong? I have checked here. But ended in run-time sql query error.
public ArrayList<Contact> fuzzySearch(String match) {
private static final String SELECTION = Phone.NUMBER + " LIKE ? COLLATE NOCASE";
Uri uri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(match));
String[] projectionFields = new String[]{ContactsContract.Contacts._ID, Phone.NUMBER};
CursorLoader cursorLoader = null;
ArrayList<Contact> listContacts = new ArrayList<>();
if(match.trim().length() == 0){
return listContacts;
}else {
cursorLoader = new CursorLoader(context, uri, projectionFields, SELECTION, new String[]{"%"+match+"%"}, Phone.NUMBER);
}
Cursor c = cursorLoader.loadInBackground();
final Map<String, Contact> contactsMap = new HashMap<>(c.getCount());
if (c.moveToFirst()) {
int idIndex = c.getColumnIndex(ContactsContract.Contacts._ID);
int nameIndex = c.getColumnIndex(Phone.NUMBER);
do {
String contactId = c.getString(idIndex);
String contactDisplayName = c.getString(nameIndex);
Contact contact = new Contact(contactId, contactDisplayName);
contactsMap.put(contactId, contact);
listContacts.add(contact);
} while (c.moveToNext());
}
c.close();
return listContacts;
}
use PhoneLookup instead of phone.
because you already encode the search key into the URL, you don't need SELECTION, remove it.
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(match));
Cursor cur = resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME,PhoneLookup.NUMBER, PhoneLookup.CONTACT_ID}, null, null, null);

How to read latest SMS from all contacts efficiently?

I am trying to read latest SMS from All contacts but the procedure Currently I am using seems to be very slow. How can I improve it?
public Map<String, SMS> getLatestMsgs(){
Uri uriSMSURI = Uri.parse("content://sms/");
String[] projection = {_id, address, body,
read, date, subscriptionId, type};
String selection = "";
String[] selectionArgs = {};
String sortOrder = Telephony.Sms.DATE +" desc";
Cursor c = context.getContentResolver()
.query(uriSMSURI, projection, selection, selectionArgs, sortOrder);
LinkedHashMap<String, SMS> smsMap = new LinkedHashMap<>();
log.info(methodName, "Reading Messages..");
if(c==null){
log.info(methodName, "Query returned null cursor");
return smsMap;
}
while (c.moveToNext()) {
String id = c.getString(c.getColumnIndexOrThrow(this._id));
String from = c.getString(c.getColumnIndexOrThrow(this.address));
String body = c.getString(c.getColumnIndexOrThrow(this.body));
int serviceCenter = c.getInt(c.getColumnIndexOrThrow(this.subscriptionId));
boolean readState = c.getInt(c.getColumnIndex(this.read)) == 1;
long time = c.getLong(c.getColumnIndexOrThrow(this.date));
long type = c.getLong(c.getColumnIndexOrThrow(this.type));
if(!smsMap.containsKey(from)) {
SMS sms = new SMS();
sms.setId(id);
sms.setFrom(from);
sms.setBody(body);
sms.setRead(readState);
sms.setDateTime(time);
sms.setType(type);
sms.setSubscription(serviceCenter);
smsMap.put(from, sms);
}
}
c.close();
return smsMap;
}
This method has following steps:
Read all sms in decreasing order of timestamp
Start Iterating over fetched rows
If sms address already in map then ignore
else put SMS into LinkedHashMap

android sms contact name

I have been searching for days now on why this is not working. I am trying to get the sender number and name of the text message, but it always returns as zero or it's always the same exact number no matter who text me. And the name is always blank.
Cursor cur = mContext.getContentResolver().query(uriReceive, null, null, null,null);
while (cur.moveToNext())
{
int index_Address = cur.getColumnIndex("address");
int index_Person = cur.getColumnIndex("person");
strAddress = cur.getString(index_Address);
intPerson = cur.getString(index_Person);
}
If you're reading existing SMSes (not just incoming new ones)
String columns[] = new String[] { "_id", "date", "address", "subject", "body" };
Cursor cursor = ctx.getContentResolver().query(Uri.parse("content://sms/inbox"), columns, whereClause, bindVars, "date desc");
//Only want newest, so only handle first item in cursor
if(cursor.moveToFirst()){
do{
int id = cursor.getInt(0);
long date = cursor.getLong(1);
String address = cursor.getString(2);
String subject = cursor.getString(3);
String body = cursor.getString(4);
//Handle message here
}while(cursor.moveToNext())
}
cursor.close();
Then to turn address into a name, you'd need to cross-reference address with the contacts database.

How to display music name by playlist name Android programming

There are some similar questions have been posted here, I have used those suggestions and tried it in different ways, but still no results for getting out, my codes is like this:
private Cursor getPlaylists(String playlistName) {
Cursor cursor = null;
String[] projection1 = { MediaStore.Audio.Playlists._ID,
MediaStore.Audio.Playlists.NAME };
cursor = this.managedQuery(
MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, projection1,
MediaStore.Audio.Playlists.NAME + "=\"" + playlistName + "\"", null,
null); // I need to put "" for the string, otherwise sqlite errors for no such table
startManagingCursor(cursor);
cursor.moveToFirst();
String playlist_id = cursor.getString(0);
Long playlist_id2 = cursor.getLong(0);
if (playlist_id2 > 0) {
String[] projection = {
MediaStore.Audio.Playlists.Members.AUDIO_ID,
MediaStore.Audio.Playlists.Members.ARTIST,
MediaStore.Audio.Playlists.Members.TITLE,
MediaStore.Audio.Playlists.Members._ID
};
cursor = getContentResolver().query(MediaStore.Audio.Playlists.Members.getContentUri("external",playlist_id2),
projection,
null,
null,
null);
}
return cursor;
But after these codes, how can I exactly get music list names? does then store in projection, but my projection variable are always no music list information inside. If I need to iterate this cursor again to get music list names, how can I do it, I have tried this way.. but it won't work.
// startManagingCursor(cursor);
for (boolean hasItem = cursor.moveToFirst(); hasItem; hasItem = cursor
.moveToNext()) {
String musicName = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Playlists.Members.TITLE));
Log.i(LOGGING_TAG, musicName);
}
it looks like hasItem is null as always. I am stuck on this for couple of hours, maybe that is a stupid question but any suggestions would be graceful.
If I understand you correctly, you want to list the tracks on a given playlist.
This is how I do it. Once you have your cursor, you just loop around it by getting the values you need.
see examples below :
public Cursor getPlaylistTracks(Context context, Long playlist_id) {
Uri newuri = MediaStore.Audio.Playlists.Members.getContentUri("external",playlist_id);
ContentResolver resolver = context.getContentResolver();
String _id = MediaStore.Audio.Playlists.Members._ID;
String audio_id = MediaStore.Audio.Playlists.Members.AUDIO_ID;
String artist = MediaStore.Audio.Playlists.Members.ARTIST;
String album = MediaStore.Audio.Playlists.Members.ALBUM;
String title = MediaStore.Audio.Playlists.Members.TITLE;
String duration = MediaStore.Audio.Playlists.Members.DURATION;
String location = MediaStore.Audio.Playlists.Members.DATA;
String composer = MediaStore.Audio.Playlists.Members.COMPOSER;
String playorder = MediaStore.Audio.Playlists.Members.PLAY_ORDER;
String date_modified = MediaStore.Audio.Playlists.Members.DATE_MODIFIED;
String[] columns = { _id, audio_id, artist, album, title, duration,
location, date_modified, playorder, composer };
Cursor cursor = resolver.query(newuri, columns, null, null, null);
return cursor;
}
if (cursor!= null){
cursor.moveToFirst();
int artistColumn = cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.ARTIST);
int durationColumn = cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.DURATION);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
{
artist = cursor.getString(artistColumn);
duration = duration + cursor.getLong(durationColumn);
// do something else
}
cursor.close();
}

Getting name of the message sender

Please tell me how can i get the name of message sender. i tried following .All field works fine but but name always 0. Please give me solution.
String SORT_ORDER = "date DESC";
int count = 0;
String[] str= new String[] { "_id", "thread_id", "address", "person", "date", "body" };
Cursor cursor = getContentResolver().query(
Uri.parse("content://sms"),
str,
null,
null,
SORT_ORDER);
Log.e("!!!!!!!!", ""+cursor.getCount());
while (cursor.moveToNext()) {
try {
// count = cursor.getCount();
long messageId = cursor.getLong(0);
long threadId = cursor.getLong(1);
String address = cursor.getString(2);
long contactId = cursor.getLong(3);
String contactId_string = String.valueOf(contactId);
long timestamp = cursor.getLong(4);
String body = cursor.getString(5);
Log.e("!!!!!!!!", "number"+address);
Log.e("!!!!!!!!", "name"+contactId_string);
I think you will get number of message sender which you have to search in PhoneLookup
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME} .....)

Categories

Resources