One, do not use permission suspension window
Implementation ‘com. Making. Doonkey: DkFloatingView: 1.0’
Project address: github.com/Doonkey/DkF…
Initialization, I put it in BaseApplication
WindowManager.LayoutParams params = new WindowManager.LayoutParams(); params.width = WindowManager.LayoutParams.MATCH_PARENT; params.height = WindowManager.LayoutParams.WRAP_CONTENT; params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; params.gravity = Gravity.BOTTOM; params.y = 100; With (this)// Application context.setLayoutid (r.layout.popupWindow)// Float the layout //.setFilter(test1_1Activity.class)// Filter activity.setLayoutParam (params)// Set the suspension layout layoutParam.build ();Copy the code
According to
FloatWindow.get().show(); / / show FloatWindow. The get (). Hide (); / / hideCopy the code
Two, the suspension window need permission
Use the permission hover window
import android.content.Context; import android.graphics.PixelFormat; import android.os.Build; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.WindowManager; import android.widget.TextView; /** * public class WindowUtils {private static final String LOG_TAG = "WindowUtils"; private static View mView = null; private static WindowManager mWindowManager = null; private static Context mContext = null; public static Boolean isShown = false; Public interface NeedShowTipWindow {/** * some pages should not be displayed, override this method, return false */ Boolean needShow(); } public static void showPopupWindow(final context) ** @param pageNeedShow context, boolean pageNeedShow) { if (! pageNeedShow) { return; } if (isShown) { // Log.i(LOG_TAG, "return cause already shown"); return; } isShown = true; Log.i(LOG_TAG, "showPopupWindow"); / / get application Context mContext = Context. GetApplicationContext (); MWindowManager = (WindowManager) context.getSystemService(context.window_service); mView = setUpView(context); final WindowManager.LayoutParams params = new WindowManager.LayoutParams(); // Set window type if (build.version.sdk_int >= build.version_codes.m) {//6.0+ params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY; } else { params.type = WindowManager.LayoutParams.TYPE_PHONE; } / / set the flag int flags = WindowManager. LayoutParams. FLAG_NOT_FOCUSABLE; // | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; / / if set the WindowManager. LayoutParams. FLAG_NOT_FOCUSABLE, pop-up View of Back key events params. Flags = flags; // Do not set this popup's clear mask to appear as black params.format = pixelFormat.always; // FLAG_NOT_TOUCH_MODAL non-blocking events are passed to the following window // Set FLAG_NOT_FOCUSABLE when the suspension window is small, the following application icon is changed from not long press to long press. Delimit the home page of the screen there will be a question params. Width = WindowManager. LayoutParams. MATCH_PARENT; params.height = WindowManager.LayoutParams.WRAP_CONTENT; params.gravity = Gravity.BOTTOM; params.y = 100; mWindowManager.addView(mView, params); Log.i(LOG_TAG, "add view"); } public static void hidePopupWindow() {log. I (LOG_TAG, "hide "+ isShown + "," + mView);} public static void hidePopupWindow() {log. I (LOG_TAG, "hide "+ isShown + "," + mView); if (isShown && null ! = mView) { Log.i(LOG_TAG, "hidePopupWindow"); mWindowManager.removeView(mView); isShown = false; } } private static View setUpView(final Context context) { Log.i(LOG_TAG, "setUp view"); View view = LayoutInflater.from(context).inflate(R.layout.popupwindow, null); TextView positiveBtn = view.findViewById(R.id.tv_circle_tips); positiveBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i(LOG_TAG, "ok on click"); / / hide the pop-up WindowUtils. HidePopupWindow (); }}); TextView close = view.findViewById(R.id.tv_close); close.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i(LOG_TAG, "cancel on click"); WindowUtils.hidePopupWindow(); }}); // Click on the outer area of the window to eliminate // This point is realized mainly by setting the suspension window to the full-screen size, with a transparent background outside, Final View popupWindowView = view.findViewById(R.i.d.popup_window); // Final View popupWindowView = view.findViewById(R.I.D.popup_window); SetOnTouchListener (new view.onTouchListener () {// @override // public Boolean onTouch(view v, MotionEvent event) { // Log.i(LOG_TAG, "onTouch"); // int x = (int) event.getX(); // int y = (int) event.getY(); // Rect rect = new Rect(); // popupWindowView.getGlobalVisibleRect(rect); // if (! rect.contains(x, y)) { // WindowUtils.hidePopupWindow(); // } // Log.i(LOG_TAG, "onTouch : " + x + ", " + y + ", rect: " // + rect); // return false; / / / /}}); // // Click back to delete // view.setonKeyListener (new view.onKeyListener () {// @override // public Boolean onKey(view v, int keyCode, KeyEvent event) { // switch (keyCode) { // case KeyEvent.KEYCODE_BACK: // WindowUtils.hidePopupWindow(); // return true; // default: // return false; //} //} //}); return view; }}Copy the code
popupwindow.xml
<? The XML version = "1.0" encoding = "utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FEFCEC" android:orientation="horizontal"> <TextView android:id="@+id/tv_circle_tips" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:marqueeRepeatLimit="marquee_forever" android:paddingStart="10dp" android:paddingTop="5dp" android:paddingEnd="10dp" android:paddingBottom="5dp" android:singleLine="true" Android :text=" #FF6E0E" Android :textColor="#FF6E0E" Android :text=" 13sp" /> <! --android:drawableEnd="@drawable/ic_next"--> <! --android:drawablePadding="@dimen/dp_16"--> <TextView android:id="@+id/tv_close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="X" /> </LinearLayout>Copy the code
Check the permission on the main page
If (build.version.sdk_int >= build.version_codes.m) {if (! Settings. CanDrawOverlays (this)) {// Request permission to lays(); @targetAPI (build.version_codes.m) private void getOverlayPermission() {Intent Intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); intent.setData(Uri.parse("package:" + getPackageName())); startActivityForResult(intent, 0); }Copy the code
Add a display method to the base class to display all pages
import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import com.example.simpledemo.WindowUtils; public class BaseActivity extends AppCompatActivity implements WindowUtils.NeedShowTipWindow {// @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override protected void onResume() { super.onResume(); WindowUtils.showPopupWindow(this, needShow()); } @Override public boolean needShow() { return true; }}Copy the code
Monitor APP in the foreground or background, foreground display, background hide
import android.app.Activity; import android.app.Application; import android.os.Bundle; import com.example.simpledemo.WindowUtils; import com.example.simpledemo.util.Utils; public class BaseApplication extends Application { public int count; @Override public void onCreate() { super.onCreate(); registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() { @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) {} @override public void onActivityStarted(Activity Activity) { If (count == 0) {Utils.LogD("CMUApplication into foreground "); if (! WindowUtils.isShown) { WindowUtils.showPopupWindow(getApplicationContext(), true); } } count++; } @Override public void onActivityResumed(Activity activity) { } @Override public void onActivityPaused(Activity activity) { } @Override public void onActivityStopped(Activity activity) { count--; If (count == 0) {Utils.LogD("CMUApplication into foreground "); if (WindowUtils.isShown) { WindowUtils.hidePopupWindow(); } } } @Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) { } @Override public void onActivityDestroyed(Activity activity) { } }); }}Copy the code