I sit, for example, in Skype ... And here comes a message Viber over cyp. Or even the screen of the phone is extinguished, still the window of the vibe will appear ...
What mechanism is used?
I sit, for example, in Skype ... And here comes a message Viber over cyp. Or even the screen of the phone is extinguished, still the window of the vibe will appear ...
What mechanism is used?
I do not know how in Viber, but this can be done using the usual AlertDialog
register in the manifest permission to display system windows
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Software unlock requires permission
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
Before displaying the dialog, put a flag to the window
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
And that's all.
You can play with this example.
public class LocalService extends Service { Handler handler = new Handler(); @Override public int onStartCommand(Intent intent, int flags, int startId) { init(); return START_STICKY; } @Nullable @Override public IBinder onBind(Intent intent) { return null; } private void init(){ handler.postDelayed(task, 10000); } Runnable task = new Runnable() { @Override public void run() { showDialog(); handler.postDelayed(this, 10000); } }; private void showDialog(){ KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE); KeyguardManager.KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE); lock.disableKeyguard(); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Test"); builder.setNegativeButton("Cancel", null); AlertDialog dialog = builder.create(); dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); dialog.show(); } } You will receive an alert every 10 seconds whether the application is launched independently or not.
Do not leave the phone with the service running :-)
Source: https://ru.stackoverflow.com/questions/446695/
All Articles