2
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,

ant2009
  • 27,094
  • 154
  • 411
  • 609
  • `android:showOnLockScreen` is for notifications. AFAIK there is no API for the apps that can be started directly from the lock screen, this is dependent of the launcher that is installed on the device. – Gerald Schneider Jun 22 '15 at 05:39
  • possible duplicate of [How to show dialog even if the screen is locked?](http://stackoverflow.com/questions/21727278/how-to-show-dialog-even-if-the-screen-is-locked) – Nir Alfasi Jun 22 '15 at 05:44
  • @alfasin not really a duplicate of this, he wants to show the app icon on the lock screen, not a dialog – Gerald Schneider Jun 22 '15 at 06:00

1 Answers1

2

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"/>
shhp
  • 3,653
  • 2
  • 18
  • 23