[英]How do I detect touch input on the Android when application is running in the background
I want to detect if a particular gesture was made in some other screen (Not when the app's UI is open) . 我想检测是否在其他屏幕上做出了特定手势(不是在打开应用程序的UI时) 。
I have seen it in some phones, you make a 'C' kind of gesture and the camera will open. 我已经在某些手机中看到它,您做出“ C”式手势,相机将打开。 Is there something like this in Android Studio?
Android Studio中是否有类似的东西?
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. 如您所知,有些应用程序正在使用此技能,例如Facebook Messenger。
You can handle touch event in setOnTouchListener callback implementation of FloatingService.kt. 您可以在FloatingService.kt的setOnTouchListener回调实现中处理触摸事件。
Please refer my source code. 请参考我的源代码。
[FloatingService.kt] [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] [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] [MainActivity.java]
I used activity to start FloatingService but you can start this service with other ways. 我使用活动来启动FloatingService,但是您可以通过其他方式来启动该服务。
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] [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} 您可以在[设定]-> [应用程式]->选择{您的应用程式名称}中找到此选项
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.