Android消息计数器

时间:2013-03-17 16:10:47

标签: android

大家好我正在尝试实现一个应用程序,该应用程序将获取联系人姓名,并将搜索从该特定联系人姓名收到的消息数量,并显示计数。但是我在我的pgm运行期间遇到了意想不到的问题。这是我的代码

 package com.mycompany.myapp;

import android.app.*;
import android.net.Uri;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import android.media.*;
import android.telephony.*;
import java.util.*;

import android.content.*;
import android.graphics.*;
import android.database.*;
import android.provider.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    DatePicker datepicker;
    private static Context context;
    Map contactMap;
    String selection;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setTitle("Tuxian");
        setContentView(R.layout.main);
        MainActivity.context = getApplicationContext();

        GetPeopleList people = null;
        final AutoCompleteTextView txt = (AutoCompleteTextView) findViewById(R.id.autocompletetxtview);

        txt.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
                selection = (String)parent.getItemAtPosition(position);
                //TODO Do something with the selected text
            }
        });

        List list = displayContacts(context);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, list);
        txt.setAdapter(adapter);
        txt.setTextColor(Color.GREEN);
        datepicker = (DatePicker) findViewById(R.id.datePicker);
        Button button = (Button) findViewById(R.id.btn);

        button.setOnClickListener(new View.OnClickListener() {

                @Override public void onClick(View v)
                {
                    Toast.makeText(getBaseContext(), "Date : " + (datepicker.getMonth() + 1) + "/" + datepicker.getDayOfMonth() + "/" + datepicker.getYear(), Toast.LENGTH_LONG).show();
                    Toast.makeText(getBaseContext(), "Total : " + msgCnt(selection), Toast.LENGTH_SHORT).show();
                }
            });
    }
    private List displayContacts(Context c) {
        ContentResolver cr = c.getContentResolver();
        Cursor pCur = null;
        contactMap = new HashMap<String, String>();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        List l = new ArrayList();
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                if (Integer.parseInt(cur.getString( cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    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));
                        //Toast.makeText(getBaseContext(), "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
                        l.add(name);                    
                        contactMap.put(name, phoneNo);
                    }
                }
            }
        }
        return l;
    }
    private int msgCnt (String phNum) {

         Uri uri = Uri.parse("content://sms/inbox");

         ContentResolver contentResolver = context.getContentResolver();

         String where = "address="+phNum;
         Cursor cursor = contentResolver.query(uri, new String[] { "_id", "thread_id"}, where, null,
                                               null);
         int count = cursor.getCount();

        return count;
    }
}

我在Manifest文件中添加了如下权限:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mycompany.myapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk 
        android:minSdkVersion="8" 
        android:targetSdkVersion="11" />
    <uses-permission android:name="android.permission.READ_SMS"/>

    <application

        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".MainActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
</manifest>

请有人帮我搞清楚!提前致谢

1 个答案:

答案 0 :(得分:0)

究竟是什么问题?我建议更改getMsgCount函数以使用以下内容:

    String str_query = "content://sms/inbox";
    Uri uri = Uri.parse(str_query);
    String where = "address = ?";
    String columns[] = new String[] {"_id", "thread_id", "msg_count"}; 
    Cursor c = context.getContentResolver().query(uri, columns, where, new String[]{phNum}, null);

请注意this,因为它确实在很多时候取决于OEM以及如何设置其联系人通讯录。我相信你真正想做的就是获取msg_count列。

但是我不确定地址是否是正确的列。我知道,如果我是正确的,你应该在短信收件箱中查看。