android 弹悬浮窗

作者: 幸福的程序媛 | 来源:发表于2015-11-20 09:56 被阅读656次

    在android中显示悬浮窗可以开启一个服务来实现,代码如下:

    publicclassFloatServiceextendsService{

    publicFloatService() {

    }

    @Override

    publicvoidonCreate() {

    super.onCreate();

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

    WindowManager.LayoutParamslayoutParams=newWindowManager.LayoutParams();

    layoutParams.type=WindowManager.LayoutParams.TYPE_TOAST;

    layoutParams.width=300;

    layoutParams.height=300;

    layoutParams.gravity=Gravity.TOP|Gravity.RIGHT;

    Buttonbutton=newButton(getApplicationContext());

    button.setText("i am float button");

    layoutParams.flags=WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

    windowManager.addView(button,layoutParams);

    }

    @Override

    publicIBinderonBind(Intent intent) {

    //TODO: Return the communication channel to the service.

    thrownewUnsupportedOperationException("Not yet implemented");

    }

    }

    注意layoutParams.type=WindowManager.LayoutParams.TYPE_TOAST;这中形式的是不需要权限的,

    layoutParams.type=WindowManager.LayoutParams.TYPE_PHONE;是需要申请

    android.permission.SYSTEM_ALERT_WINDOW权限,跟上篇在application下面弹对话框需要的权限是一样的,其中估计是有很大联系的,后面再研究dialog的实现原理。备忘。

    相关文章

      网友评论

        本文标题:android 弹悬浮窗

        本文链接:https://www.haomeiwen.com/subject/zymlhttx.html