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?
I had a similar requirement while designing my music player. What I finally end up doing for my lock screen widget is:
You can have a look at it by downloading my musicplayer "Fusion Music Player" from Google Play
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)
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);
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.