我正在尝试为我的应用创建自定义建议。它运作良好,但现在我正在尝试解决一个问题。我已经android:searchSuggestIntentAction="android.intent.action.VIEW"
在 AndroidManifest中设置了
<intent-filter>
<action android:name="android.intent.action.VIEW" />
</intent-filter> to Activity but even if I did this, it still sends default Intent - `ACTION_SEARCH`.
你有什么想法,问题可能出在哪里?
我的安卓清单
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<provider
android:authorities="com.example.animalist.SuggestionProvider"
android:name=".SuggestionProvider" >
</provider>
<meta-data android:name="android.app.default_searchable"
android:value=".SearchActivity"/>
<activity
android:name="com.example.animalist.MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait" >
<meta-data android:name="android.app.default_searchable"
android:value=".SearchActivity"/>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.animalist.MoreActivity"
android:label="@string/title_activity_more"
android:launchMode="singleTop"
android:parentActivityName="com.example.animalist.MainActivity"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.animalist.MainActivity" />
</activity>
<activity
android:name="com.example.animalist.SearchActivity"
android:label="@string/title_activity_search"
android:launchMode="singleTop"
android:parentActivityName="com.example.animalist.MainActivity"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.animalist.MainActivity" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
</application>
我的searchable.xml
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint"
android:searchSuggestAuthority="com.example.animalist.SuggestionProvider"
android:searchSuggestSelection=" ?"
android:searchSuggestIntentAction="android.intent.action.VIEW"
/>
我的SuggestionsProvider的一部分
public class SuggestionProvider extends SearchRecentSuggestionsProvider {
private static final String TAG = "SuggestionProvider";
private static final int SEARCH_SUGGESTIONS = 1;
private static final UriMatcher sURLMatcher = new UriMatcher(
UriMatcher.NO_MATCH);
static {
sURLMatcher.addURI("*", SearchManager.SUGGEST_URI_PATH_QUERY,
SEARCH_SUGGESTIONS);
sURLMatcher.addURI("*", SearchManager.SUGGEST_URI_PATH_QUERY + "/*",
SEARCH_SUGGESTIONS);
}
private static final String[] COLUMNS = new String[] {
"_id",
SearchManager.SUGGEST_COLUMN_TEXT_1,
SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
SearchManager.SUGGEST_COLUMN_QUERY,
SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA
};
public SuggestionProvider() {
}
@Override
public boolean onCreate() {
return true;
}
@Override
public Cursor query(Uri url, String[] projectionIn, String selection,
String[] selectionArgs, String sort) {
int match = sURLMatcher.match(url);
switch (match) {
case SEARCH_SUGGESTIONS:
String query = url.getLastPathSegment();
MatrixCursor cursor = new MatrixCursor(COLUMNS);
ParseQuery<Animal> squery = ParseQuery.getQuery(Animal.class);
try {
List<Animal> results = squery.find();
for (Animal animal : results) {
addRow(cursor, animal.getAnimal());
} }catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return cursor;}
return null;
}
private void addRow(MatrixCursor cursor, String string) {
long id = cursor.getCount();
cursor .newRow().add(id).add(string).add(Intent.ACTION_SEARCH).add(string);
}
@Override
public String getType(Uri url) {
int match = sURLMatcher.match(url);
switch (match) {
case SEARCH_SUGGESTIONS:
return SearchManager.SUGGEST_MIME_TYPE;
default:
throw new IllegalArgumentException("Unknown URL: " + url);
}
}
@Override
public int update(Uri url, ContentValues values, String where, String[] whereArgs) {
throw new UnsupportedOperationException("update not supported");
}
@Override
public Uri insert(Uri url, ContentValues initialValues) {
throw new UnsupportedOperationException("insert not supported");
}
@Override
public int delete(Uri url, String where, String[] whereArgs) {
throw new UnsupportedOperationException("delete not supported");
}
}
我的搜索活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
mActionBar = getSupportActionBar();
mActionBar.setDisplayHomeAsUpEnabled(true);
mListView = (ListView) findViewById(R.id.search_listview);
mProgressBar = (ProgressBar) findViewById(R.id.search_loading_animals);
Intent intent = getIntent();
nAdapter = new SearchAdapter(SearchActivity.this, new ArrayList<Animal>());
mProgressBar.setVisibility(View.VISIBLE);
mListView.setVisibility(View.VISIBLE);
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra("query").toString();
Toast.makeText(getBaseContext(), "search", Toast.LENGTH_SHORT).show();
doSearch(query);
}
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
Toast.makeText(getBaseContext(), "view", Toast.LENGTH_SHORT).show();
}
}
public void doSearch(String query){
mListView = (ListView) findViewById(R.id.search_listview);
nAdapter = new SearchAdapter(SearchActivity.this, new ArrayList<Animal>());
View header = getLayoutInflater().inflate(R.layout.search_header, null);
mListView.addHeaderView(header, null , false);
result = (TextView) header.findViewById(R.id.result);
result.setText(" '"+query+"'");
mListView.setDividerHeight(0);
mListView.setAdapter(nAdapter);;
ParseQuery<Animal> squery = ParseQuery.getQuery(Animal.class);
squery.whereMatches("animal", query, "i");
squery.findInBackground(new FindCallback<Animal>() {
@Override
public void done(List<Animal> animals, ParseException error) {
if(animals != null){
nAdapter.clear();
for (int i = 0; i < animals.size(); i++) {
mProgressBar = (ProgressBar) findViewById(R.id.search_loading_animals);
mProgressBar.setVisibility(View.INVISIBLE);
nAdapter.add(animals.get(i));
}
}
}
}); } }
对于任何答案或建议,我都会很高兴。我试图解决这个问题很长时间。
提前致谢