Monday, February 11, 2013

Widgets

Handling Home Key in Android

I recently came across "Todler lock" application which had lot of interesting features. One of the most intriguing feature was handling  of Home key event.

1.Create a Activity Alias

We use Activity alias in order to programmatically enable or disable home screen launcher. An alias of an activity acts as an independent entity and intent filters that are not in the original activity can be added.

Declare your activity and declare an alias for your activity with the following intent filters


[xml]
<activity
android:name="com.vkslabs.example.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-alias
android:name=".MainActivityAlias"
android:targetActivity="com.vkslabs.example.MainActivity"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY"/>
</intent-filter>
</activity-alias>
[/xml]

2. Enabling and disabling component using setComponentEnabledSetting
[java]
PackageManager manager = getPackageManager();
ComponentName compName = new ComponentName("com.vkslabs.example", "com.vkslabs.example.MainActivityAlias");
//ComponentName compName = new ComponentName("<Package Name>", "<Package Name.Alias Name>");

manager.setComponentEnabledSetting(compName,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
[/java]
You can use COMPONENT_ENABLED_STATE_DISABLED for disabling the alias.

No comments:

Post a Comment