원본 : http://skyswim42.egloos.com/2982153
상세 설명 : http://underclub.tistory.com/356
-- SearchLauncher.java --
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SearchLauncher extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
final Button btn = (Button)findViewById(R.id.search);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onSearchRequested();
}
});
}
public boolean onSearchRequested(){
return super.onSearchRequested();
}
}
-- SearchTest.java --
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class SearchTest extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
if(intent.getAction().equals(Intent.ACTION_SEARCH)){
String query = intent.getStringExtra(SearchManager.QUERY);
Toast.makeText(this, query, Toast.LENGTH_SHORT).show();
}
}
}
==========================================================
-- searchable.xml -- ( xml dir )
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/search_label"
android:icon="@drawable/icon"
android:hint="@string/search_hint"
android:searchMode="showSearchIconAsBadge"
/>
주의사항 : @string/xxx 대신 text 를 직접 입력하면 안됨.
==========================================================
SearchManager 구현 시 주의 깊게 봐야 하는 것이 Manifest 인 것 같다.
1) 검색어를 자신이 받아서 처리하느냐?
2) 다른 activity 에게 넘기느냐?
3) browser 검색으로 넘기느냐?
이 세가지가 결정되는 것으로 보인다.
-- AndroidManifest.xml --
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SearchLauncher"
android:label="SearchLauncher">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- 아래와 같이 하면 browser search 가 실행된다.
<meta-data android:name="android.app.default_searchable"
android:value="*" />
-->
</activity>
<activity android:name=".SearchTest"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<meta-data android:name="android.app.default_searchable"
android:value=".SearchTest" />
</application>