[Q] How can I change the layout for a widget in runtime?

Search This thread

saman0suke1

Senior Member
Feb 14, 2011
244
8
Hi, I have a widget that I want to use as lockscreen as well ( since android 4.2+ allows it), so I need to change the layout when the widget is on the lock screen and when is on the regular screen, how can i do it? I tried to use the Intent ACTION_USER_PRESENT but my BroadcastReceiver never gets it. I have it defined in the manifest as well:

PHP:
    <receiver
        android:name="com.myApp.myApp.MyWidgetIntentReceiver"
        android:exported="false"
        android:label="widgetBroadcastReceiver" >
        <intent-filter> 
            <action android:name="android.intent.action.USER_PRESENT" >
            </action>                               
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/demo_widget_provider" />
    </receiver>

public class MyWidgetIntentReceiver extends BroadcastReceiver{

    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(Intent.ACTION_USER_PRESENT){
            Log.i("TICK", intent.getAction());          
        }
    }

}

Is there any other way that I can check when the widget is in the lock screen or not? I tried this guide as well:

http://developer.android.com/guide/topics/appwidgets/index.html#lockscreen

But how do I check that every time that I lock/unlock the screen?

Thanks!

EDIT: I ended up using Intent.ACTION_USER_PRESENT to check when the screen is unlocked, Thanks!
 
Last edited: