4

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?

Cilenco
  • 6,951
  • 17
  • 72
  • 152
  • 1
    Are you expecting to see the `Lockscreen`(partially) behind the activity? – Vikram Aug 14 '13 at 06:46
  • 1
    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 Aug 14 '13 at 07:43
  • I hope someone can help you out. But, most likely, this will not work. – Vikram Aug 14 '13 at 07:47
  • 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 Aug 15 '13 at 12:41
  • 1
    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 Aug 15 '13 at 21:01
  • have u implemented getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); ??? – Yogesh Tatwal Aug 19 '13 at 08:20
  • Yes I have that's the code from line 4 – Cilenco Aug 19 '13 at 08:30
  • I have the exact requirement. And I'm facing the same problems that you have faced. Did you find any answer? – Shirish Herwade May 20 '14 at 14:15

4 Answers4

0

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

Sushil
  • 8,250
  • 3
  • 39
  • 71
0

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

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);
Shirish Herwade
  • 11,461
  • 20
  • 72
  • 111
-1

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);
Community
  • 1
  • 1
Phil
  • 35,852
  • 23
  • 123
  • 164
  • But with this method it is not possible to unlock the screen while the activity is visible. Or can I create something to click throught the activity? – Cilenco Aug 19 '13 at 15:41
  • @Cilenco I have not dealt much with `Window`s, and do not know for sure. The same question was asked [here](http://stackoverflow.com/questions/11635934/touch-through-translucent-app), and has an answer that may work. Also, check out the flags listed [here](http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html), which may change how the window is inserted (above or below soft input, etc). – Phil Aug 19 '13 at 15:55
  • @Cilenco please tell me, did this solution worked, because i tired this, and even in this case(fix height), lockscreen not getting displayed behind it, rather launcher screen gets displayed behind my activity – Shirish Herwade May 20 '14 at 14:36