Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

android - How can one detect screen gestures outside ones own app?

I know that SO frowns heavily on "how do I do this" questions, but I don't see a way around it this time.

I'm a seasoned and expert Android developer so I feel really goofy asking this question, but I just installed this app and it does something that I thought was impossible.

Namely, it listens for screen gestures (in this case swipes from edges of the screen inward) REGARDLESS of where you are on your device... meaning, it listens when you're on the Launcher home screens, and when you're in OTHER apps... no matter what you're doing, it's listening and when it detects the swipe from the edge of the screen it lets you bring out a hidden settings drawer which then lives as a transparent (fragment? dialog?) View on top of whatever other app you're in and when dismissed (by hitting BACK) leaves you wherever you were in your previous experience.

I honestly have no clue how this is possible and would really love a nudge in the right direction. [EDIT]enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

there are 2 things you have to handle:

  1. showing a view on top. for this, you need to use a special permission TYPE_SYSTEM_ALERT . here's a code for showing a view on top:

    final WindowManager.LayoutParams param=new WindowManager.LayoutParams();
    param.flags=WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
    final View view=findViewById(R.id.view1);
    final ViewGroup parent=(ViewGroup)view.getParent();
    if(parent!=null)
      parent.removeView(view);
    param.format=PixelFormat.RGBA_8888;
    param.type=WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
    param.gravity=Gravity.TOP|Gravity.LEFT;
    param.width=view.getLayoutParams().width;
    param.height=view.getLayoutParams().height;
    final WindowManager wmgr=(WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
    wmgr.addView(view,param);
    // TODO handle overlapping title bar and/or action bar
    // TODO you must add logic to remove the view
    // TODO you must use a special permission to use this method :android.permission.SYSTEM_ALERT_WINDOW
    
  2. keeping the app alive. for this, you need to create a service that runs in the foreground, which means it uses a notification. here's a link.

there are plenty of apps that have this ability. the first one i've seen was AirCalc.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...