Is there a way of determining the number of total messages in our device.
I've tried the following for finding the number of calls:
Uri allCalls = Uri.parse("content://call_log/calls");
Cursor c = managedQuery(allCalls, null, null, null, null);
count= c.getCount();
Log.i("TAG",Integer.toString(count));
But I'm not able to view any number when i tried this similar to messages in my logcat.
Try this code to get all Messages
public List<String> getSMS(){
List<String> sms = new ArrayList<String>();
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, null);
while (cur != null && cur.moveToNext()) {
String address = cur.getString(cur.getColumnIndex("address"));
String body = cur.getString(cur.getColumnIndexOrThrow("body"));
sms.add("Number: " + address + " .Message: " + body);
}
if (cur != null) {
cur.close();
}
return sms;
}
Just Call the method and you will receive an ArrayList of all messages
Get List of Messages in Inbox Like Below:
First Add Permission in Manifest.xml to read sms:
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
public ArrayList<String> fetchInbox()
{
ArrayList<String> sms = new ArrayList();
//Read Draft message only by "content://sms/draft"
//Read Sent message only by "content://sms/sent"
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor cursor = getContentResolver().query(uriSms, new String[]{"_id", "address", "date", "body"},null,null,null);
cursor.moveToFirst();
while (cursor.moveToNext())
{
String address = cursor.getString(1);
String body = cursor.getString(3);
sms.add(body);
}
return sms;
}
And You can get SMS count by sms.size();
Related
Hey so I just made an android app which basically fetches all the mp3/wav files from your phone and then shows them it to you. Which you can then play them. But I am experiencing slow load times. My phone has like 10 mp3 and its a 6GB RAM phone. But it takes like 1 minute to load all those files. I am using the built in MediaPlayer Class. Any tips?
Use this code.
for reference link
public void getMp3Songs() {
Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
songsList.clear();
Cursor cursor = managedQuery(allsongsuri, null, selection, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String song_name = cursor
.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
int song_id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media._ID));
String fullpath = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DATA));
songsList.add(fullpath);
String album_name = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM));
int album_id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
String artist_name = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST));
int artist_id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
} while (cursor.moveToNext());
}
cursor.close();
}
Toast.makeText(context, ""+songsList.size(), Toast.LENGTH_SHORT).show();
}
i have a huge problem with duplicated contacts.
After sorting array with:
Collections.sort(mAllContacts);
I'm reading contacts with :
ContentResolver cr = mContext.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
if (cur.getInt(cur.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
contacts = new AllContacts(name, phoneNo);
mAllContacts.add(contacts);
}
pCur.close();
}
}
}
if (cur != null) {
cur.close();
}
With this way, all contact are retrieved to a list (Local storage, Sim, Gmail etc).
I have no problem to remove duplicated contact by name like this:
for (int i = 0; i < mAllContacts.size() - 1; i++) {
if (mAllContacts.get(i).getmContactName().equals(mAllContacts.get(i + 1).getmContactName())) {
Log.d("duplicatedArray", "setAdapter: " + mAllContacts.get(i).getmContactName());
mAllContacts.remove(i+1);
}
}
but it's not a good practice because some times different contacts may have same name,
so i can remove duplicated contact with same method but use:
mAllContacts.get(i + 1).getmPhoneNumber()
And here is the problems comes:
For some reason , the phone number have different format while reading from gmail, local storage , sim.
For ex.
Gmail phone number : +972-54-333-55-88
Local storage phone number : +972-543335588
Sim : +972543335588
How can i solve my problem to remove duplicated values.
And yes , i need to read contact from all places where they appear (gmail, local storage , sim)
Apply regex to change the same phone numbers to a common format e.g.
class Main {
public static void main(String args[]) {
String p1 = "+972-54-333-55-88";
String p2 = "+972-543335588";
String p3 = "+972543335588";
String p4 = "+97(2543)335588";
String p5 = "+97 2543 335588";
String regex = "[^0-9+]";
System.out.println(p1.replaceAll(regex, ""));
System.out.println(p2.replaceAll(regex, ""));
System.out.println(p3.replaceAll(regex, ""));
System.out.println(p4.replaceAll(regex, ""));
System.out.println(p5.replaceAll(regex, ""));
}
}
Output:
+972543335588
+972543335588
+972543335588
+972543335588
+972543335588
Normalize
I think you should normalize these phone numbers before you store them.
So for example all of these:
Gmail phone number : +972-54-333-55-88
Local storage phone number : +972-543335588
Sim : +97254335588
Could just be stored as 97254335588 You can parse these numbers by removing all space, hyphens, and plus signs.
HashSet
It would also be easier to store these in a HashSet so you don't even need to worry about removing duplicates.
In my project I need to access sms which received after given time. For example it can be get all sms which I received after 1538633990932 . This time is in millis. I make the project which getting all messages in inbox. but i need to access sms received after specific time.
List<String> sms = new ArrayList<String>();
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cur = c.getContentResolver().query(uriSMSURI, null, null, null, null);
while (cur != null && cur.moveToNext()) {
String address = cur.getString(cur.getColumnIndex("address"));
String body = cur.getString(cur.getColumnIndexOrThrow("body"));
}
if (cur != null) {
cur.close();
}
I think I need to declare the time in the query.
You have to pass the epoch value in the filter.
String filter = "date>=" + 1538633990932;
Cursor cur = c.getContentResolver().query(uriSMSURI, null, filter, null, null);
Taken from this post.
I have an activity that has a listview which displays all contacts with multi-selection enabled. I have also a filter to filter the display result.
After struggling with this problem for 3 days, I finally could produce a list of all display names selected by the user.
My question now is how to get the corresponding email and phone number for each display name in my list?
Update:
I found a solution..
public String getNumber(String name,Context context){
String number="";
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor people = context.getContentResolver().query(uri, projection, null, null, null);
int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
people.moveToFirst();
do {
String Name = people.getString(indexName);
String Number = people.getString(indexNumber);
if(Name.equalsIgnoreCase(name)){return Number.replace("-", "");}
// Do work...
} while (people.moveToNext());
if(!number.equalsIgnoreCase("")){return number.replace("-", "");}
else return number;
}
public String getEmail(String name,Context context){
String email="";
Uri uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA};
Cursor people = context.getContentResolver().query(uri, projection, null, null, null);
int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
people.moveToFirst();
do {
String Name = people.getString(indexName);
String mail = people.getString(indexNumber);
if(Name.equalsIgnoreCase(name)){return mail;}
// Do work...
} while (people.moveToNext());
if(!email.equalsIgnoreCase("")){return email;}
else return email;
}
Got it from there:
Android get contact number using name
How can I get an exact count of MMS inbox and Sent box in ANdroid?
Uri mmsInboxUri = Uri.parse("content://mms/inbox");
Cursor mmsInboxCursor = getContentResolver().query(mmsInboxUri ,new String[] {"_id","ct_t"}, null, null, null);
int count = mmsInboxCursor.getCount();
if (mmsInboxCursor != null)
{
if (mmsInboxCursor.moveToFirst())
{
coloumns = mmsInboxCursor.getColumnNames();
//String subject = mmsInboxCursor.getString(mmsInboxCursor.getColumnIndex("sub"));
String string = mmsInboxCursor.getString(1);
if ("application/vnd.wap.multipart.related".equals(string))