android 弹悬浮窗

在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 弹悬浮窗)