I am running this code after every 30 second and after running app coninute 1 hour my phone and app hung up and i need to restart it again.
private void preventStatusBarExpansion(Context context) {
WindowManager manager = ((WindowManager)
context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE));
Activity activity = (Activity) context;
WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
localLayoutParams.gravity = Gravity.TOP;
localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
// this is to enable the notification to recieve touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
// Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
int resId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
int result = 0;
if (resId > 0) {
result = activity.getResources().getDimensionPixelSize(resId);
}
localLayoutParams.height = result;
localLayoutParams.format = PixelFormat.TRANSPARENT;
CustomViewGroup view = new CustomViewGroup(context);
manager.addView(view, localLayoutParams);
}
public class CustomViewGroup extends ViewGroup {
public CustomViewGroup(Context context) {
super(context);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.v("customViewGroup", "**********Intercepted");
return true;
}
}
You're effectively calling the following calls every 30 seconds.
CustomViewGroup view = new CustomViewGroup(context);
manager.addView(view, localLayoutParams);
This creates and adds a view every 30 seconds, so it will surely scrape out the memory when being run for long durations. You should clearly remove the view once it is not required. Otherwise you're repeatedly stacking up the memory with the new views.
Related
How can I disable users can swipe down the title/notification bar as seen in the picture below. I want my app run in kiosk mode and my users must not be able to swipe down or anything else.
I tried using setting some flags but all I get is some kind of kiosk mode but users can still view the wifi/airplane etc screen on swipe down.
Please help, I can not find a good answer on Stackoverflow
I don not want my users can see the screen below.
Refer to this answer here
or
private void disablePullNotificationTouch() {
WindowManager manager = ((WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE));
WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
localLayoutParams.gravity = Gravity.TOP;
localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
// this is to enable the notification to recieve touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
// Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
localLayoutParams.height = (int) (25 * getResources()
.getDisplayMetrics().scaledDensity);
localLayoutParams.format = PixelFormat.RGBX_8888;
customViewGroup view = new customViewGroup(this);
manager.addView(view, localLayoutParams);
}
//Add this class in your project
public class customViewGroup extends ViewGroup {
public customViewGroup(Context context) {
super(context);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.v("customViewGroup", "**********Intercepted");
return true;
}
I have added a view with special params using windowmanager, it overlays the screen and I am trying to change the alpha of this view using a seekbar. I have tried changing the alpha of a button just to test my code, and it works properly, but when I try to change the alpha of the overlay view, it just changes colour to a transparent black (it is normally transparent pink or yellow). I was hoping to change the opacity of the yellow or pink, but it does not seem to work that way. I think it is because of the special params, or the fact that it is added using windowmanager. The float alpha = intent.getExtras().getFloat("alpha"); is passed down from my main activity and is the progress of my seekbar(max = 100)/100 so it ends up being a value of 0.0 to 1.0. If anybody has any ideas let me know, thanks. Here is the service:
`public class DrawOverAppsService extends Service {
public static final String TAG = "DrawOverAppsService";
View mOverlayView;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate");
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN|
WindowManager.LayoutParams.FLAG_DIM_BEHIND,
PixelFormat.TRANSLUCENT);
// An alpha value to apply to this entire window.
// An alpha of 1.0 means fully opaque and 0.0 means fully transparent
params.alpha = 0.2F;
// When FLAG_DIM_BEHIND is set, this is the amount of dimming to apply.
// Range is from 1.0 for completely opaque to 0.0 for no dim.
params.dimAmount = 0.5F;
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mOverlayView = inflater.inflate(R.layout.overlay_view, null);
wm.addView(mOverlayView, params);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (mOverlayView != null) {
Boolean isBlack = intent.getExtras().getBoolean("black");
Boolean isPink = intent.getExtras().getBoolean("pink");
Boolean isOrange = intent.getExtras().getBoolean("orange");
Boolean isAlpha = intent.getExtras().getBoolean("isAlpha");
if(isBlack == true) {
mOverlayView.setBackgroundColor(Color.parseColor("#000000"));
}
if(isPink == true) {
mOverlayView.setBackgroundColor(Color.parseColor("#3d0d00"));
}
if(isOrange == true) {
mOverlayView.setBackgroundColor(Color.parseColor("#3d2f00"));
}
if (isAlpha == true) {
float alpha = intent.getExtras().getFloat("alpha");
mOverlayView.getBackground().setAlpha((int)alpha);
//mOverlayView.setAlpha(intent.getExtras().getFloat("alpha"));
}
}
return START_NOT_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
wm.removeView(mOverlayView);
}
}`
Open()
mOverlayView .animate().scaleX(1.0f).scaleY(1.0f).setDuration(200)
.start();
mOverlayView .setAlpha(0.5f);
Close()
mOverlayView .animate().scaleX(0f).scaleY(0f).setDuration(200).start();
I have spend like 2 hours,
I'm unable to figure out what is the issue in service. I'm not calling stopService() or stopSelf from anywhere else. Below is the code ,
public class FloatingViewService extends Service {
private WindowManager mWindowManager;
private View mFloatingView;
WindowManager.LayoutParams params,landscapeParams,nonTouchableParams;
public FloatingViewService() {
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
//Inflate the floating view layout we created
mFloatingView = LayoutInflater.from(this).inflate(R.layout.layout_floating_widget, null);
//Add the view to the window.
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
//Specify the view position
params.gravity = Gravity.TOP | Gravity.LEFT; //Initially view will be added to top-left corner
params.x = 0;
params.y = 100;
//Add the view to the window.
landscapeParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
//Specify the view position
landscapeParams.gravity = Gravity.TOP | Gravity.LEFT; //Initially view will be added to top-left corner
landscapeParams.x = 0;
landscapeParams.y = 100;
//Add the view to the window.
nonTouchableParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
PixelFormat.TRANSLUCENT);
//Specify the view position
nonTouchableParams.gravity = Gravity.TOP | Gravity.LEFT; //Initially view will be added to top-left corner
nonTouchableParams.x = 0;
nonTouchableParams.y = 100;
//Add the view to the window
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mFloatingView, params);
//The root element of the collapsed view layout
final View collapsedView = mFloatingView.findViewById(R.id.collapse_view);
//The root element of the expanded view layout
final View expandedView = mFloatingView.findViewById(R.id.expanded_container);
//Set the close button
ImageView closeButtonCollapsed = (ImageView) mFloatingView.findViewById(R.id.close_btn);
closeButtonCollapsed.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//close the service and remove the from from the window
stopSelf();
}
});
//Set the close button
ImageView closeButton = (ImageView) mFloatingView.findViewById(R.id.close_button);
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
collapsedView.setVisibility(View.VISIBLE);
expandedView.setVisibility(View.GONE);
}
});
//Set the close button
ImageView lock = (ImageView) mFloatingView.findViewById(R.id.lock_button);
lock.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mWindowManager.updateViewLayout(mFloatingView, nonTouchableParams);
}
});
//Set the close button
ImageView expand = (ImageView) mFloatingView.findViewById(R.id.expand);
expand.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mWindowManager.updateViewLayout(mFloatingView, landscapeParams);
}
});
//Drag and move floating view using user's touch action.
mFloatingView.findViewById(R.id.root_container).setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//remember the initial position.
initialX = params.x;
initialY = params.y;
//get the touch location
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_UP:
int Xdiff = (int) (event.getRawX() - initialTouchX);
int Ydiff = (int) (event.getRawY() - initialTouchY);
//The check for Xdiff <10 && YDiff< 10 because sometime elements moves a little while clicking.
//So that is click event.
if (Xdiff < 10 && Ydiff < 10) {
if (isViewCollapsed()) {
//When user clicks on the image view of the collapsed layout,
//visibility of the collapsed layout will be changed to "View.GONE"
//and expanded view will become visible.
collapsedView.setVisibility(View.GONE);
expandedView.setVisibility(View.VISIBLE);
}
}
return true;
case MotionEvent.ACTION_MOVE:
//Calculate the X and Y coordinates of the view.
params.x = initialX + (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
//Update the layout with new X & Y coordinate
mWindowManager.updateViewLayout(mFloatingView, params);
return true;
}
return false;
}
});
}
/**
* Detect if the floating view is collapsed or expanded.
*
* #return true if the floating view is collapsed.
*/
private boolean isViewCollapsed() {
return mFloatingView == null || mFloatingView.findViewById(R.id.collapse_view).getVisibility() == View.VISIBLE;
}
#Override
public void onDestroy() {
super.onDestroy();
if (mFloatingView != null) mWindowManager.removeView(mFloatingView);
}
}
I am not able to figure out anomalous behaviour help me out.
Start your service as forground.
https://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)
Since you want your service to run continuously you will have to override the onStartCommand method and return START_STICKY
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
For started services, there are two additional major modes of operation they can decide to run in, depending on the value they return from onStartCommand(): START_STICKY is used for services that are explicitly started and stopped as needed, while START_NOT_STICKY or START_REDELIVER_INTENT are used for services that should only remain running while processing any commands sent to them. See the linked documentation for more detail on the semantics.
https://developer.android.com/reference/android/app/Service.html
I am trying to block status bar and I am successful in doing that by adding a view to status bar. I am trying to remove the added view when onClick is detected, but the view still remains and I am not able to remove the view. Can any one please let me know what am I missing. The current code is as shown below:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
manager = ((WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE));
localLayoutParams = new WindowManager.LayoutParams();
localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
localLayoutParams.gravity = Gravity.TOP;
localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
// this is to enable the notification to recieve touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
// Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
localLayoutParams.height = (int) (50 * getResources()
.getDisplayMetrics().scaledDensity);
localLayoutParams.format = PixelFormat.TRANSPARENT;
view = new customViewGroup(this);
manager.addView(view, localLayoutParams);
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
m_activateButton = (Button)findViewById(R.id.button);
m_activateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(manager != null){
manager.removeView(view);
}
}});
}
CustomView Class:
public class customViewGroup extends ViewGroup {
public customViewGroup(Context context) {
super(context);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.v("customViewGroup", "**********Intercepted");
return true;
}
}
I am building a new lock screen for Android, but I am unable to lock the notification bar from pulling it down.
I want to disable the notification bar pull-down.
private void disablePullNotificationTouch() {
WindowManager manager = ((WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE));
WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
localLayoutParams.gravity = Gravity.TOP;
localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
// this is to enable the notification to recieve touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
// Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
localLayoutParams.height = (int) (25 * getResources()
.getDisplayMetrics().scaledDensity);
localLayoutParams.format = PixelFormat.RGBX_8888;
customViewGroup view = new customViewGroup(this);
manager.addView(view, localLayoutParams);
}
//Add this class in your project
public class customViewGroup extends ViewGroup {
public customViewGroup(Context context) {
super(context);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.v("customViewGroup", "**********Intercepted");
return true;
}
}
Unless you modify system SystemUI.apk file, it's not possible, even then your app will require root permissions.
The best you could do is create an app in fullscreen mode which will hide notification bar.
Firstly, you need the EXPAND_STATUS_BAR permission:
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
and hope this link will help you for sure..