4
votes

I want to display an Activity in front of the Lockscreen. I tried with this code in my onCreate medthod:

super.onCreate(savedInstanceState);
Window window = getWindow();

window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);        
setContentView(R.layout.test);

That works great for Activities which are displayed over the whole screen but I want to build an Activity which is only 200px heigh. I thought I can match it with this code:

super.onCreate(savedInstanceState);
Window window = getWindow();

LayoutParams layout = new WindowManager.LayoutParams();
layout.copyFrom(window.getAttributes());
layout.height = 200;

window.setAttributes(layout);
window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);

setContentView(R.layout.test);

But then the Activity is not visible in front of the lockscreen. If I unlock the phone the Activity is displayed directly. Any ideas why and how I can fix this?

EDIT: I think it has something to do with the window floating FLAG. If this flag is set (and I think it will when I change the size) the activity is not displayed over the lockscreen. But this is only a presumption. Any ideas or a workaround to fix this?

4
Are you expecting to see the Lockscreen(partially) behind the activity?Vikram
Yes that is the problem. I want to see the lockscreen behind the activity and may want to unlock the phone while the activity is over the lockscreen (if that is possible)Cilenco
I hope someone can help you out. But, most likely, this will not work.Vikram
I found a workaround this (even though it's not tested yet): KeyguardManager myKeyGuard = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE); myLock = myKeyGuard.newKeyguardLock("tagName"); myLock.disableKeyguard(); - with the permission in the Manifest android.permission.DISABLE_KEYGUARD. Then alternatively try Window window = getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);.g00dy
Thanks for your help but that is not want I want. The first solution disabled the lockscreen completly. The second shows the ativity after I unlocked the phone but not in front of the lockscreen. I really want to see the lockscreen behind the activity and may want to unlock the phone while the activity is over the lockscreen (if that is possible)Cilenco

4 Answers

0
votes

I had a similar requirement while designing my music player. What I finally end up doing for my lock screen widget is:

  1. Had a transperent background for my Lock screen widget.
  2. added a button "Click to exit to lockscreen" at the bottom of layout.
  3. To give a feel of box, added a white boundary around the buttons.

You can have a look at it by downloading my musicplayer "Fusion Music Player" from Google Play

0
votes

First of all save to preferences Keyguard instanse.

After that - disable programmatically default lockscreen on your phone.

Then, implements Service which catch when phone go to lock state.

When SCREEN_ON check Activity in stack, if she exist show her, NO - launch by singleTask.

It's a single way to implementing Activity on Lockscreen. Because Google reject make-up lockscreens and any manipulating with this things are hack's.

You can make widget and put him to lockscreen or below writing way.

FYI: google it my lock for android (Source on code.google.com)

0
votes

Finally I achieved the same. Don't go for activity, because android will not show lock screen behind your activity for security reason, so for service.

Below is my code in onStartCommand of my service

WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

View mView = mInflater.inflate(R.layout.score, null);

WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT, 0, 0,
        WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
        /* | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON */,
        PixelFormat.RGBA_8888);

mWindowManager.addView(mView, mLayoutParams);
-1
votes

Your best bet will be to create a View that fills the screen, but has a transparent background everywhere other than the part that you want.

This can be done by defining a custom theme in the file res/values/styles.xml (originally shown on How do I create a transparent Activity on Android?):

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:backgroundDimEnabled">false</item>
  </style>
</resources>

Then adding this theme to your Activity's Manifest declaration:

android:theme="@style/Theme.Transparent"

Next, you can create the layout (R.layout.lockscreen) for this Activity by adding this XML file. Here you specify the height in pixels:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <FrameLayout 
        android:layout_width="match_parent"
        android:layout_height="200px"
        android:centerInParent="true" >

        <include layout="@layout/test" /> <!-- This points to your R.layout.test layout. -->

    </FrameLayout>

</RelativeLayout>

Finally, just use the first method shown above to show the Activity:

super.onCreate(savedInstanceState);
Window window = getWindow();

window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);        
setContentView(R.layout.lockscreen);