0

I want to detect if a particular gesture was made in some other screen (Not when the app's UI is open).

I have seen it in some phones, you make a 'C' kind of gesture and the camera will open. Is there something like this in Android Studio?

jbmcle
  • 771
  • 3
  • 12
Rana
  • 19
  • 6
  • You are asking like " If i touch Whatsapp chat how I can detect it in Gmail App" – Zaid Mirza Nov 25 '18 at 05:05
  • Something similar. I want to use one gesture anywhere in my phone and call an Intent of my app. So that i can perform action according to the gesture. I know this will be major security concern but I am trying to make this app for myself and to be used in my own phone, So I don't mind giving the permission (if needed). –  Rana Nov 25 '18 at 07:44
  • Please, I have the same issue where I need to implement gesture touch data from anywhere in my phone and call my background running service to collect them. Can you help me with this? please check my question here https://stackoverflow.com/questions/65405516/touch-event-getaction-does-not-work-under-service-class-in-android-studio?noredirect=1#comment115787432_65405516 – Mohsen Ali Jan 04 '21 at 02:17

1 Answers1

1

You can do that using Service that is running on background.

As you know, there is some applications which are using this skill such as Facebook messenger.

You can handle touch event in setOnTouchListener callback implementation of FloatingService.kt.

Please refer my source code.


[FloatingService.kt]

package com.antasis9.android.playground

import android.app.Service
import android.content.Context
import android.content.Intent
import android.graphics.PixelFormat
import android.os.IBinder
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.Button

class FloatingService : Service() {
        override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
                Log.d("FloatingService", "FloatingService.onStartCommand()");

                val layout = LayoutInflater.from(this).inflate(R.layout.activity_floating, null)
                layout.setOnTouchListener { v, event ->

                        // HANDLE TOUCH EVENT HERE!

                        Log.d("FloatingService", "v: $v, event: $event")
                        false
                }

                val layoutParams = WindowManager.LayoutParams(
                                WindowManager.LayoutParams.MATCH_PARENT,
                                WindowManager.LayoutParams.MATCH_PARENT,
                                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                                PixelFormat.TRANSLUCENT
                )

                (getSystemService(Context.WINDOW_SERVICE) as WindowManager).addView(layout, layoutParams)

                return super.onStartCommand(intent, flags, startId)
        }

        override fun onBind(intent: Intent): IBinder? {
                return null
        }
}

[activity_floating.xml]

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent">
</android.support.constraint.ConstraintLayout>

[MainActivity.java]

I used activity to start FloatingService but you can start this service with other ways.

package com.antasis9.android.playground;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                Intent intent = new Intent(getApplicationContext(), FloatingService.class);
                                startService(intent);
                        }
                });

                findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                Intent intent = new Intent(getApplicationContext(), FloatingService.class);
                                stopService(intent);
                        }
                });
        }
}

[AndroidManifest.xml]

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.antasis9.android.playground">

        <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

        <application
                android:allowBackup="true"
                android:icon="@mipmap/ic_launcher"
                android:label="@string/app_name"
                android:roundIcon="@mipmap/ic_launcher_round"
                android:supportsRtl="true"
                android:theme="@style/AppTheme">
                <activity android:name=".MainActivity">
                        <intent-filter>
                                <action android:name="android.intent.action.MAIN" />

                                <category android:name="android.intent.category.LAUNCHER" />
                        </intent-filter>
                </activity>

                <service
                        android:name=".FloatingService"
                        android:enabled="true"
                        android:exported="false"></service>
        </application>
</manifest>

[This is last step]

You should turn on 'display over other apps' option.

You can find this option 'Settings' -> 'Apps' -> Select {your app name}

Turn on 'display over other apps' option

Seungmin Ha
  • 151
  • 1
  • 5