Split your <intent-filter>
into 2 separate Activity
s like this:
<activity
android:name="com.bill.deuterh.SearchButtonActivity"
android:theme="@style/Theme.Transparent"
android:label="@string/title_activity_search_button" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity-alias
android:targetActivity="com.bill.deuterh.SearchButtonActivity"
android:name="com.bill.deuterh.SearchButtonActivityAlias"
android:enabled="false"
android:label="@string/title_activity_search_button">
<intent-filter>
<action android:name="android.intent.action.SEARCH_LONG_PRESS"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity-alias>
You'll notice that the alias has enabled="false"
which means that the default state of this Activity
is disabled. In this state it won't show up in the search long press options list.
When you want to enable the search activity so that it shows up in the list, you just need to enable the alias component using the PackageManager
, like this:
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(new ComponentName(this,
"com.bill.deuterh.SearchButtonActivityAlias"),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
If you want to disable it again, you can do it like this:
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(new ComponentName(this,
"com.bill.deuterh.SearchButtonActivityAlias"),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);