Last active
October 21, 2018 05:08
-
-
Save libxzr/158f532a7b3918edaa215d31a79875eb to your computer and use it in GitHub Desktop.
Android floating window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
button.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if(Build.VERSION.SDK_INT>=23){ | |
if(Settings.canDrawOverlays(context)){ | |
context.startService(new Intent(MainActivity.THIS, PerfmonService.class)); | |
} | |
else { | |
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); | |
intent.setData(Uri.parse("package:" + context.getPackageName())); | |
context.startActivity(intent); | |
} | |
} | |
else{ | |
context.startService(new Intent(MainActivity.THIS, PerfmonService.class)); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void init(){ | |
params=new WindowManager.LayoutParams(); | |
windowManager=(WindowManager)getApplication().getSystemService(Context.WINDOW_SERVICE); | |
if(Build.VERSION.SDK_INT>=26){ | |
params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY; | |
} | |
else{ | |
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; | |
} | |
params.format = PixelFormat.RGBA_8888; | |
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; | |
params.gravity = Gravity.LEFT | Gravity.TOP; | |
params.x = 0; | |
params.y = 0; | |
params.width = 500; | |
params.height = 300; | |
main= (LinearLayout)LayoutInflater.from(this).inflate(R.layout.float_perfmon,null); | |
textView=main.findViewById(R.id.perfmon1); | |
linearLayout1=main.findViewById(R.id.perfmonl1); | |
textView.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
stopSelf(); | |
if(LBuild.DEBUG) | |
Log.d(TAG,"Tried to stop"); | |
} | |
}); | |
windowManager.addView(main,params); | |
main.measure(View.MeasureSpec.UNSPECIFIED,View.MeasureSpec.UNSPECIFIED); | |
int resourceId = getResources().getIdentifier("status_bar_height","dimen","android"); | |
if (resourceId > 0) { | |
statusBarHeight = getResources().getDimensionPixelSize(resourceId); | |
} | |
main.setOnTouchListener(new View.OnTouchListener() { | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
params.x = (int) event.getRawX()-main.getWidth()/2; | |
params.y = (int) event.getRawY() - statusBarHeight-main.getHeight()/2; | |
windowManager.updateViewLayout(main,params); | |
return false; | |
} | |
}); | |
new refreshInfo().start(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment