Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
626 views
in Technique[技术] by (71.8m points)

android - How to create a custom lock screen widget (I just want to display a button)

I need to allow users to quickly capture an image using my app when the device is locked. I figure the quickest way for a user to do this is via a button/widget on the lock screen - although I'm not sure how to build this.

Most references I've found have related to music playback and use of the RemoteControlClient (which may be just Android 4.4?). At it's most basic I'd just like one button that said "capture". Any help on how to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

API Levels

Lock-screen widgets were introduced in API 17 (4.2), and removed in API 21 (5.0). They are not supported on other official releases.


Basic Widget

I wrote a simple widget as a demo tutorial - it contains all the boilerplate code required for a widget, and very little else:

I wrote it in such a way to make it easy for anyone to remove the "wifi" related code, and adapt it to their own widget requirements. It might be perfect for you to look at, and relatively simple to add a single button to it.


Lock-screen / Keyguard Widget

There are 2 changes to make it work as a lock-screen widget:

  • updating the widgetCategory to include keyguard
  • adding an initialKeyguardLayout

These changes are done in the ./res/xml/widget_info.xml file, as seen below:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialKeyguardLayout="@layout/widget"
    android:initialLayout="@layout/widget"
    android:minHeight="40dp"
    android:minWidth="250dp"
    android:updatePeriodMillis="0"
    android:widgetCategory="home_screen|keyguard" >
</appwidget-provider>

I do not know if it is possible to integrate the camera into your own lock-screen widget. Clicking on a lock-screen widget normally requires the user to unlock the device before the click works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...