Skip to content

Instantly share code, notes, and snippets.

@wangmuy
Last active August 30, 2016 09:53
Show Gist options
  • Save wangmuy/8446932645551499d5633ec4e104b438 to your computer and use it in GitHub Desktop.
Save wangmuy/8446932645551499d5633ec4e104b438 to your computer and use it in GitHub Desktop.
android.service.overlayWindow
// prepare
// AndroidManifest.xml add permission
// <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
// see http://stackoverflow.com/questions/7569937/unable-to-add-window-android-view-viewrootw44da9bc0-permission-denied-for-t
WindowManager mWm = (WindowManager) getSystemService(WINDOW_SERVICE);
mTextView = new TextView(this);
mLayoutParam = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
PixelFormat.OPAQUE);
mLayoutParam.gravity = Gravity.CENTER;
mLayoutParam.setTitle("AlertWindow from service");
// add and update
if(false == mTextView.isAttachedToWindow()) {
mWm.addView(mTextView, mLayoutParam);
}
mTextView.setText("hello world");
mTextView.invalidate();
// remove
if(mTextView.isAttachedToWindow()) {
mWm.removeView(mTextView);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment