如何从给定名称获取建议的联系人列表

时间:2013-12-14 12:54:55

标签: android android-contacts

我正在为我的大学项目开发​​一个Android应用程序。想法是开发一个语音命令基地来电应用程序。到现在为止,我可以实现此应用程序来获取语音名称并从联系人列表中检索联系人。但这只有在我为语音提供正确的语音输入时才能工作。所以我计划根据我的应用程序的语音输入从手机中提供联系人列表的建议。我怎么能这样做?

    package com.chamika_kasun.voicerecognitionapp;

    import java.util.ArrayList;
    import java.util.List;
    //import java.util.List;

    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.ContactsContract;
    import android.provider.ContactsContract.CommonDataKinds.Phone;
    import android.app.Activity;
    import android.content.ContentResolver;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.database.Cursor;
    //import android.content.pm.PackageManager;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.Toast;
    import android.speech.RecognizerIntent;
    import android.content.pm.ResolveInfo;

    public class MakeCall extends Activity implements OnClickListener {

        public ListView mList;
        public Button speakButton;

        public static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.make_call);

            speakButton = (Button) findViewById(R.id.btn_speak);
            speakButton.setOnClickListener(this);

            voiceinputbuttons();

            // Check to see if a recognition activity is present
            // if running on AVD virtual device it will give this message. The mic
            // required only works on an actual android device
              // Disable button if no recognition service is present
            PackageManager pm = getPackageManager();
            List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
            if (activities.size() == 0)
            {
                speakButton.setEnabled(false);
                speakButton.setText("Recognizer not present");
            }

        }


        public void voiceinputbuttons() {
            speakButton = (Button) findViewById(R.id.btn_speak);
            mList = (ListView) findViewById(R.id.list);
        }

        public void startVoiceRecognitionActivity() {

            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"Speech recognition demo");
            startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);

        }

        public void onClick(View v) {

            startVoiceRecognitionActivity();

        }

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
                // Fill the list view with the strings the recognizer thought it
                // could have heard
                ArrayList matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                mList.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, matches));

                //if (matches.contains("vidudaya")) {

                    String name = matches.toString();
                    String number = "";


                    Toast.makeText(this, "Name : "+name,Toast.LENGTH_SHORT).show();

                    ContentResolver cr = getContentResolver();
                    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                        "DISPLAY_NAME = '" + name + "'", null, null);
                    if (cursor.moveToFirst()) {
                        String contactId =
                            cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                        //
                        //  Get all phone numbers.
                        //
                        Cursor phones = cr.query(Phone.CONTENT_URI, null,
                            Phone.CONTACT_ID + " = " + contactId, null, null);
                        while (phones.moveToNext()) {
                           number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
    //                      int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
    //                      switch (type) {
    //                          case Phone.TYPE_HOME:
    //                              // do something with the Home number here...
    //                              break;
    //                          case Phone.TYPE_MOBILE:
    //                              // do something with the Mobile number here...
    //                              break;
    //                          case Phone.TYPE_WORK:
    //                              // do something with the Work number here...
    //                              break;
    //                          }

                            Toast.makeText(this, "My Mobile Number : "+number,Toast.LENGTH_SHORT).show();

                        }
                        phones.close();
                       }
                       cursor.close();

                        String url = "tel:"+number;
                        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
                        startActivity(intent);

                //}


                super.onActivityResult(requestCode, resultCode, data);
            }
        }

    }

1 个答案:

答案 0 :(得分:1)

您需要在联系人列表中创建一个反向索引(通过字母或N-Gram),并在收到用户命令后对其进行查询。

你可以做一些关于最佳答案的建议:

Partial search in HashMap