ant2009
ant2009

Reputation: 22556

How to show your app on the lockscreen?

Android Studio 1.3 Preview 5

Hello,

I want to display my app's icon on the users lockscreen. Example, like you have the camera app shortcut. I would like to do the same for my app. So the user will click or drag the app icon and the App will open.

There a property in the manifest under the activity element, that I think should work. However, when I install my app on a Samsung running Kitkat 4.4.2 it didn't work (API 17 or above).

android:showOnLockScreen="true"

In the following online documentation I couldn't see this property:

http://developer.android.com/guide/topics/manifest/activity-element.html

Just wondering if this is possible or not.

Many thanks for any suggestions,

Upvotes: 2

Views: 4336

Answers (1)

shhp
shhp

Reputation: 3693

Here is the way I come up with. When your app detect currently in the lock screen use following code snippet to add your app icon to the screen.

WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
    lp.gravity = Gravity.CENTER;
    lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
    lp.format = PixelFormat.TRANSLUCENT;
    lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
    ImageView image = new ImageView(this);
    image.setImageResource(R.mipmap.ic_launcher);
    image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Do something here
        }
    });
    wm.addView(image, lp);

Declare this permission in manifest:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

Upvotes: 2

Related Questions