【问题标题】:How to get an contact count and media card count in android?如何在 android 中获取联系人计数和媒体卡计数?
【发布时间】:2011-08-07 10:35:17
【问题描述】:

有谁知道如何通过程序获取android手机上的联系人数量?我真的需要那个..所以它会像这样烤:

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

我想通过程序为我的 android 手机创建报告,现在我只能获取手机中 SMS 的数量。代码是这样的:

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();

    }

}

请帮我解决我的问题..提前谢谢..

【问题讨论】:

    标签: java android android-ndk android-manifest


    【解决方案1】:

    要获取您设备上的联系人数量,请首先确保您已在您的 android manifest 中设置此权限。

    <uses-permission android:name="android.permission.READ_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();
    

    如果您想了解更多有关检索联系信息的信息,可以查看here

    【讨论】:

    • 感谢 theisenp.. 效果很好.. :) 顺便说一句,你知道如何获取媒体卡的详细信息吗??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多