Is there a way to block touch on ANY application from a service? I've been thinking about getWindow(), but that is impossible in service. Also, I've been thinking about getting current activity and then use getWindow(). I hope you get the point of what am I tried to achieve.
Thanks to David Medenjak I have managed to solve the problem. This solution is working on Android APIs below 23 and it's going to make the screen black and block touches until you longpress on the screen.
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
ImageView black = new ImageView(this);
black.setImageResource(R.drawable.black);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 0;
windowManager.addView(black, params);
black.getLayoutParams().height = windowManager.getDefaultDisplay().getHeight();
black.getLayoutParams().width = windowManager.getDefaultDisplay().getWidth();
black.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return false;
}
});
Related
I am trying to block touch on a device using service and a transparent overlay.
The code seems to work fine on Android Oreo but doesn't work properly on devices running Android version below Oreo:
private void setWindowParams(){
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
} else {
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_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
}
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
FrameLayout noTouchUi = new FrameLayout(this);
wm.addView(noTouchUi,params);
}
Here I setup a FrameLayout noTouchUi to intercept touch as an overlay using service using onTouchListener
noTouchUi.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.d(TAG,"touch event");
return true;
}
});
This works properly on Android 8, but doesn't work on Android 7, the overlay shows up properly, but the items behind overlay gets clicked. What I want to do is block that click.
Change the type from:
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY
to:
WindowManager.LayoutParams.TYPE_PHONE
and remove flags:
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
and:
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
I need a view to show up at the top of my application, and when it's shown, it can keep showing at the top of all my application's other view(all the fragment and activity). It sounds like a Floating Action Button, but will always show at the top of my app.
I know I can do it via adding view to my phone's WindowManager, and hide it when I quit my app, show it again when I resume my app. This tricky method can work, but it also require some additional permission, which is I am trying to avoid.
If I only want to show in my app, can I achieve it without asking additional permission from user? If the answer is yes, then how? The key seems like some LayoutParams for the view, I tried but failed.
Would be nice if answer can show some detail code and explanation.
You have to use WindowManager for this purpose
First add permission in manifest
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Add image which you want to appear.
chatheadImg = (ImageView)chatheadView.findViewById(R.id.chathead_img);
Then make the service and add window manager to it.
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
And just register touch events on view
chatheadView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//To do
break;
case MotionEvent.ACTION_MOVE:
break;
});
check these tutorials for better understanding
http://www.androidhive.info/2016/11/android-floating-widget-like-facebook-chat-head/
https://github.com/henrychuangtw/Android-ChatHead
adding this permission in manifest
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutView = layoutInflater.inflate(R.layout.ringing_layout, null);
phoneTv = layoutView.findViewById(R.id.phone);
Log.d("TAG", "showLayout: " + phoneTv );
p = new WindowManager.LayoutParams(
// Shrink the window to wrap the content rather than filling the screen
600, 600,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,
PixelFormat.TRANSLUCENT);
layoutView.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
windowManager.removeView(layoutView);
}
});
p.gravity = Gravity.CENTER | Gravity.CENTER;
p.x = 0;
p.y = 0;
windowManager.addView(layoutView, p);
Log.d("TAG", "showLayout: ");
I want an imageview for my app just like fb messenger chat header which will be shown everywhere when the app is running in background and we are working on another app. I have seen some codes but they just make it visible at a fixed position not movable the code is here and i have just used textview for the time being instead of imageview.
public void onCreate() {
super.onCreate();
//mView = new HUDView(this);
mButton = new Button(this);
mButton.setText("Overlay button");
mButton.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(getApplicationContext(),"Overlay button event", Toast.LENGTH_SHORT).show();
return false;
}
});
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.RIGHT | Gravity.CENTER;
params.setTitle("Load Average");
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mButton, params);
}
Any help would be appreciated.
The link of Lalit Poptani is good. You have to use a service for this kind of feature.
Please, look the example form Waza_Be, here: https://stackoverflow.com/a/15980900/5098294
I am writing an android library that needs to display an activity on top of the client app. After some reading up it seems that adding a new window is what I want to do so I added in this permission.
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
I then try to add an image view by doing this
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.BOTTOM | Gravity.LEFT;
params.x = 10;
params.y = 100;
ImageView imageview = new ImageView(this);
imageview.setImageResource(R.drawable.avatar);
windowManager.addView( imageview , params);
But what I want is to have my own activity on this new window on top of the client apps window. is this even possible? How would I do this?
Ok the correct approach to this appears to be to inflate the layout with in the service class and add it to the windowmanager that way. Then create a regular java class that can be used as the event listeners for the actions within that layout. I didnt need to use fragments or activities.
mRootLayout = (LinearLayout) LayoutInflater.from(context).
inflate(R.layout.myContainerXML, null);
MyListManager myListManager = new MyListManager(mRootLayout,context);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
PixelFormat.TRANSLUCENT);
ListView listView = (ListView) mRootLayout.findViewById(R.id.listView);
final MyListAdapter adapter = new MyListAdapter(activity,R.layout.list_row,(ArrayList)myListManager.getList());
listView.setAdapter(adapter);
windowManager.addView(mRootLayout, params);
If anyone has a better way I would love to hear it thanks
I want to implement notifications like the one in the following image.
Notification appears any time. I think it's of course a background service waiting for new messages from the server then shows this. What I think this is an activity implemented as dialog with this custom UI. Am I correct? And is it a normal startActivity method from the service? And how do I do the transition animation to make it appears slowly from left to right with zooming when show up?
Check out this link http://www.piwai.info/chatheads-basics. He provides information about how to add them on your screen.
The trick is to add a View to the WindowManager like following code
private WindowManager windowManager;
private ImageView chatHead;
public void addView()
{
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
chatHead = new ImageView(this);
chatHead.setImageResource(R.drawable.android_head);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
windowManager.addView(chatHead, params);
}
Don't forget to add the permission <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>