T
T
TrueGraf2013-04-02 00:54:36
Android
TrueGraf, 2013-04-02 00:54:36

How to make a custom Lock Screen for Android 4?

You need to make a Lock screen, after launching which is displayed on top of the standard one (that is, it turns out two lock screens, this is important, not to replace the standard one, but to add one more). An example is the lock screen of the Poweramp player. Tell me or share a tutorial on how this is theoretically implemented.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
constv, 2013-04-02
@constv

when you turn on the screen will show a black screen for a second, permissions are required

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

public class LockService extends Service {

  PowerManager.WakeLock wl;
  WindowManager mWindowManager;
  boolean isShowing;
  View mView;
  Thread mThread;

  BroadcastReceiver mReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
      String action = intent.getAction();
      if (action.equals(Intent.ACTION_SCREEN_ON)) {

        if (isShowing) {
          if ((mThread == null)
              || ((mThread != null) && (!mThread.isAlive())))
            mThread = new Thread() {

              @Override
              public void run() {
                // TODO Auto-generated method stub
                try {
                  Thread.sleep(1000);
                  mWindowManager.removeView(mView);
                  isShowing = false;
                } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                }

              }

            };
          mThread.start();
        }
      }
      if (action.equals(Intent.ACTION_SCREEN_OFF)) {

        if (!isShowing) {
          WindowManager.LayoutParams params = new WindowManager.LayoutParams(
              WindowManager.LayoutParams.FILL_PARENT,
              WindowManager.LayoutParams.FILL_PARENT,
              WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
              WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
                  |
                  // Draws over status bar
                  WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
              PixelFormat.TRANSLUCENT);

          mWindowManager.addView(mView, params);
          isShowing = true;
        }
      }
    }

  };


  @Override
  public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public void onCreate() {

    mView = new View(this);
    mView.setBackgroundColor(Color.BLACK);

    mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
        "com.google.syncloc.SyncLocService");
    wl.acquire();
  
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_SCREEN_ON);
    intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
    //intentFilter.addAction(Intent.ACTION_USER_PRESENT);
    intentFilter.setPriority(999);
    registerReceiver(mReceiver, intentFilter);

    super.onCreate();
  }

  @Override
  public void onDestroy() {

    if (isShowing)
      mWindowManager.removeView(mView);
    if ((mThread != null) && (mThread.isAlive()))
      mThread.interrupt();

    wl.release();
    super.onDestroy();
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {

    return START_STICKY;
  }

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question