private void createFloatView(){
final Button button=new Button(getApplicationContext());
button.setText("hello");
final WindowManager windowManager=(WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.type=WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
params.format= PixelFormat.RGBA_8888;
params.flags=WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
params.width=100;
params.height=100;
// button.setOnTouchListener(new View.OnTouchListener() {
// int lastx,lasty;
// int paramx,paramy;
// @Override
// public boolean onTouch(View v, MotionEvent event) {
// switch (event.getAction()){
// case MotionEvent.ACTION_DOWN:
// lastx=(int)event.getRawX();
// lasty=(int)event.getRawY();
// paramx=params.x;
// paramy=params.y;
// break;
// case MotionEvent.ACTION_MOVE:
// int dx=(int)event.getRawX()-lastx;
// int dy=(int)event.getRawY()-lasty;
// params.x=paramx+dx;
// params.y=paramy+dy;
// windowManager.updateViewLayout(button,params);
// break;
// }
// return true;
// }
// }); windowManager.addView(button,params);
}
public final static int REQUEST_CODE = 101;
public void checkDrawOverlayPermission() {
/** check if we already have permission to draw over other apps */ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(getApplicationContext())) {
/** if not construct intent to request permission */ Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
/** request permission via start activity for result */ startActivityForResult(intent, REQUEST_CODE);
}
}
}
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
/** check if received result code is equal our requested code for draw permission */ if (requestCode == REQUEST_CODE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.canDrawOverlays(this)) {
// continue here - permission was granted createFloatView();
}
}
}
}
评论
发表评论