при включении экрана будет секунду показывать черный экран, требуются разрешения
<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;
}
}