Michael Frans
Michael Frans

Reputation: 623

How to get an contact count and media card count in android?

has anyone know how to get the contact count on android mobile phone via program?? i really need that.. so it will toast like this :

contact count : 175 contacs
media card data : music >> 6 data | image >> 7 data ect.

i want to create report for my android phone via program and for now i only can get the count of SMS in my phone.. that code is like this :

package com.application.smsandroid;

import android.app.Activity;
import android.os.Bundle;
import android.database.Cursor;
import android.net.Uri;
import android.widget.TextView;
import android.widget.Toast;

public class smsandroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView view = new TextView(this);
        Uri uriSMSURI = Uri.parse("content://sms/inbox");
        Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);
        String sms = "";
        int jumlah=0;
        while (cur.moveToNext()) {
            sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+"\n";
            jumlah=jumlah+1;
        }
        //view.setText(sms);
        setContentView(view);


        Toast.makeText(this,"SMS Count : " + Integer.toString(jumlah), 1).show();

    }

}

please help me for solving my problem.. thanks in advance..

Upvotes: 0

Views: 1569

Answers (1)

theisenp
theisenp

Reputation: 8719

To get the number of contacts on your device, first make sure that you have set this permission in your android manifest.

<uses-permission android:name="android.permission.READ_CONTACTS" />

Then you can use this to retrieve the number of contacts:

ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
int numberOfContacts = cur.getCount();
Toast.makeText(this, String.valueOf(numberOfContacts), Toast.LENGTH_SHORT).show();

If you want to learn more about retrieving contact information, you can look here.

Upvotes: 2

Related Questions