无法在Android应用程序中搜索联系人

时间:2014-04-18 05:16:21

标签: javascript android

在我的应用程序中,我需要搜索电话联系号码,我想选择一个号码,我想保存该号码,然后在我点击后,发送按钮信息应该转到该号码。这里使用了两个以上的活动,我正在搜索一个这样的活动内的联系人。

请任何人都可以帮助我......提前谢谢..

这是我的代码。

public class SearchNum extends Activity {
private TextView resultText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.searchnum);

    resultText = (TextView)findViewById(R.id.searchViewResult);

    setupSearchView();
}

private void setupSearchView() {
    SearchManager searchManager = (SearchManager)     getSystemService(Context.SEARCH_SERVICE);
    final SearchView searchView = (SearchView) findViewById(R.id.searchView);
    SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
    searchView.setSearchableInfo(searchableInfo);
}

@Override
protected void onNewIntent(Intent intent) {
    if  (ContactsContract.Intents.SEARCH_SUGGESTION_CLICKED.equals(intent.getAction())) {
        //handles suggestion clicked query
        String displayName = getDisplayNameForContact(intent);
        resultText.setText(displayName);
    } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        // handles a search query
        String query = intent.getStringExtra(SearchManager.QUERY);
        resultText.setText("should search for query: '" + query + "'...");
    }
}

private String getDisplayNameForContact(Intent intent) {
    Cursor phoneCursor = getContentResolver().query(intent.getData(), null,  null, null, null);
    phoneCursor.moveToFirst();
    int idDisplayName = phoneCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
    String name = phoneCursor.getString(idDisplayName);
    phoneCursor.close();
    return name;
}
}

我的搜索视图的xml文件是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SearchNum" >

<SearchView
    android:id="@+id/searchView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" >
</SearchView>

<TextView
    android:id="@+id/searchViewResult"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/searchView"
    android:layout_centerHorizontal="true"
    android:text="@string/hello_world" />

</RelativeLayout>

然后我在xml文件夹中的可搜索文件是

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/findContact"
android:hint="@string/findContact"
android:includeInGlobalSearch="true"
android:queryAfterZeroResults="true"
android:searchMode="queryRewriteFromText"
android:searchSettingsDescription="@string/findContact"
android:searchSuggestAuthority="com.android.contacts"
android:searchSuggestIntentAction="android.provider.Contacts.SEARCH_SUGGESTION_CLICKED"
android:searchSuggestIntentData="content://com.android.contacts/contacts/lookup" >

<!-- allow green action key for search-bar and per-suggestion clicks -->
<actionkey
    android:keycode="KEYCODE_CALL"
    android:queryActionMsg="call"
    android:suggestActionMsg="call" />

</searchable>

我也在我的清单文件中取得了许可

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

在活动中我把

 <action android:name="android.intent.action.SEARCH" />

这里也是我的元数据

  <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />

1 个答案:

答案 0 :(得分:0)

您还需要添加到清单文件中:

android:launchMode="singleTop"

链接:http://looksok.wordpress.com/2013/06/15/android-searchview-tutorial-edittext-with-phone-contacts-search-and-autosuggestion/