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

Android 如何从中获取给定姓名的建议联系人列表,android,android-contacts,Android,Android Contacts,我目前正在为我的大学项目开发一款android应用程序。这个想法是开发一个语音命令基础呼叫应用程序。到现在为止,我可以实现这个应用程序来获取语音姓名并从联系人列表中检索联系人。但这只有在我只为语音输入正确的语音时才能起作用。因此我计划根据我的应用程序的语音输入从手机的联系人列表中给出建议。我该怎么做 package com.chamika_kasun.voicerecognitionapp; import java.util.ArrayList; import java

我目前正在为我的大学项目开发一款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);
            }
        }

    }
package com.chamika_kasun.voicerecognitionapp;
导入java.util.ArrayList;
导入java.util.List;
//导入java.util.List;
导入android.net.Uri;
导入android.os.Bundle;
导入android.provider.contacts合同;
导入android.provider.contacts contract.CommonDataTypes.Phone;
导入android.app.Activity;
导入android.content.ContentResolver;
导入android.content.Intent;
导入android.content.pm.PackageManager;
导入android.database.Cursor;
//导入android.content.pm.PackageManager;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.ListView;
导入android.widget.Toast;
导入android.speech.RecognizerIntent;
导入android.content.pm.ResolveInfo;
公共类MakeCall扩展活动实现OnClickListener{
公共列表视图mList;
公共按钮speakButton;
公共静态最终int语音识别请求代码=1234;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.make_call);
speakButton=(按钮)findViewById(R.id.btn_speak);
speakButton.setOnClickListener(此);
voiceinputbuttons();
//检查是否存在识别活动
//如果在AVD虚拟设备上运行,则会显示此消息。麦克风
//required仅适用于实际的android设备
//如果没有识别服务,则禁用按钮
PackageManager pm=getPackageManager();
列表活动=pm.QueryInputActivities(新意图(识别者意图.行动\识别\言语),0);
如果(activities.size()==0)
{
speakButton.setEnabled(假);
speakButton.setText(“识别器不存在”);
}
}
公共void voiceinputbuttons(){
speakButton=(按钮)findViewById(R.id.btn_speak);
mList=(ListView)findViewById(R.id.list);
}
公共无效startVoiceRecognitionActivity(){
意向意向=新意向(识别意向、行动、识别言语);
intent.putExtra(RecognizerIntent.EXTRA语言模型,RecognizerIntent.LANGUAGE模型自由形式);
intent.putExtra(RecognizerIntent.EXTRA_提示符,“语音识别演示”);
startActivityForResult(意图、语音识别、请求、代码);
}
公共void onClick(视图v){
startVoiceRecognitionActivity();
}
@凌驾
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
if(requestCode==语音识别\请求\代码和结果代码==结果\确定){
//用识别器认为合适的字符串填充列表视图
//可以听到
ArrayList matches=data.getStringArrayListExtra(RecognizerIntent.EXTRA_结果);
mList.setAdapter(新的ArrayAdapter(这个,android.R.layout.simple_list_item_1,匹配));
//if(匹配.contains(“vidudaya”)){
字符串名称=matches.toString();
字符串编号=”;
Toast.makeText(这个,“Name:+Name,Toast.LENGTH_SHORT).show();
ContentResolver cr=getContentResolver();
Cursor Cursor=cr.query(ContactsContract.Contacts.CONTENT\u URI,null,
“显示名称='”+NAME+“'”,空,空);
if(cursor.moveToFirst()){
字符串联系人ID=
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//
//获取所有电话号码。
//
游标phones=cr.query(Phone.CONTENT\u 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));
//开关(类型){
//case Phone.TYPE_HOME:
////用这里的家庭号码做点什么。。。
//中断;
//手机壳。手机类型:
////用这里的手机号码做点什么。。。
//中断;
//case Phone.TYPE_工作:
////用这里的工作编号做点什么。。。
//中断;
//                          }
Toast.makeText(这是“我的手机号码:+Number,Toast.LENGTH_SHORT).show();
}
电话。关闭();
}
cursor.close();
String url=“电话:”+号码;
Intent=newintent(Intent.ACTION_调用,Uri.parse(url));
星触觉(意向);
//}
super.onActivityResult(请求代码、结果代码、数据);
}
}