我正在尝试创建我的第一个
Android应用程序,并在添加SEARCH功能的过程中.我已经按照Android开发人员文档添加了“搜索”对话框和小部件.不幸的是,每当我执行搜索时,都会调用搜索活动的“onCreate”和“onNewIntent”.也就是说,通过在“操作栏搜索”框中键入内容并按ENTER键,搜索称为“TWICE”.我被卡住了是否可以从可搜索的活动中返回一些全球标志,通知应用程序搜索已经完成?是否正在调用搜索对话框和小部件?
我已经搜索了这个网站上的以前的帖子和网络上没有用.感谢您的任何帮助.
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shop"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service android:name="com.shop.RestIntentService" />
<activity
android:name="com.shop.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.shop.CatalogActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.shop.SearchableActivity"
android:launchMode="singletop" >
<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>
<Meta-data
android:name="android.app.default_searchable"
android:value="com.shop.SearchableActivity" />
</application>
</manifest>
SearchableActivity.java
public class SearchableActivity extends ListActivity implements Receiver {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("onCreate");
handleIntent(getIntent());
}
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
System.out.println("onNewIntent");
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doSearch(query);
}
}
private void doSearch(String queryStr) {
System.out.println("searching..." + queryStr);
}
CatalogActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.sample,menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
//return super.onCreateOptionsMenu(menu);
return true;
}
sample.xml中
<item
android:id="@+id/menu_search"
android:actionViewClass="android.widget.SearchView"
android:icon="@android:drawable/ic_menu_search"
android:showAsAction="ifRoom|collapseActionView"
android:title="@string/menu_search"/>
解决方法
我认为NewNntent和onCreate上的方法是互斥的.你将launchMode设置为“singletop”,所以方法onNewIntent将被调用而不是onCreate.
当一个搜索请求被处理两次时,我有类似的问题.使用WXGA 10.1平板电脑仿真器中的SearchableDictionary示例,我发现第一个搜索呼叫工作正常,但随后的调用创建两个SEARCH事件,因此它们被处理两次.有人提到了Ti的bug. (http://developer.appcelerator.com/question/127166/android-search-keyboardtype-fires-return-event-twice)
我测试了一个真正的三星平板电脑上的应用程序,我没有看到两个SEARCH事件,所以我猜这是一个模拟器的问题,而不是一个代码问题.