当前位置: 首页 > 知识库问答 >
问题:

当应用程序在后台运行时,如何检测Android上的触摸输入

宋望
2023-03-14

我想检测是否在其他屏幕上做出了特定的手势(不是在应用程序的UI打开时)。

我在一些手机中见过,你做一个“C”的手势,相机就会打开。Android Studio里有这种东西吗?

共有1个答案

田翰林
2023-03-14

您可以使用在后台运行的服务来做到这一点。

如您所知,有一些应用程序正在使用此技能,例如Facebook Messenger。

可以在setOnTouchListener回调实现中处理触摸事件FloatingService.kt.

请参考我的源代码。

[浮动服务. 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>

[主要活动.java]

我使用活动来启动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);
                        }
                });
        }
}

[Android.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>

[这是最后一步]

你应该打开“在其他应用上显示”选项。

您可以找到此选项“设置”-

 类似资料:
  • 我正在做一个关于Android Studio的项目。我需要在后台服务中检测屏幕是否被触摸(并弹出一条消息)。但是,在不影响用户使用智能手机的情况下,我无法在后台服务中检测屏幕是否被触摸。 当我说“后台检测”时,我的意思是应用程序的效果不会干扰用户在屏幕上所做的事情。我下面的进度应该解释一下我在这里的意思: 我做了一个应用程序,通过在Android系统中实现onTouchListener来检测用户是

  • 问题内容: 我是一名Android开发人员,我想在我的应用程序中编写一条语句。在此语句中,我要检查默认浏览器(Android OS中 问题答案: 添加下面的Helper类: 现在,你可以从以下代码中检查所需的应用程序是否正在运行:

  • 问题内容: 我在这里查看了所有其他“自动取消”问题,这些问题似乎都包含我没有犯的错误。我都尝试过 和 都不行。 由于我的最低API为8,因此我正在使用NotificationCompat。这是我的完整代码。在此特定通知中,我并不是在调用意图,因为我不需要用户执行任何操作。 通知显示完美。您可以滑动以清除它。但是,只需点击它并不会取消该通知。它只是点亮并停留在那里。 我的代码与此处发布的其他代码之间

  • 我正在试验的Android信标库,我能够使其工作与苹果兼容的信标监测和测距添加自定义解析器(请参阅这是正确的布局,以检测iBeacons与AltBeacon的Android信标库?) 现在我正在尝试使用此处显示的示例代码编写一个在后台启动的应用程序: http://altbeacon.github.io/android-beacon-library/samples.html 这是我的代码: 不幸的

  • 问题内容: 关于此主题,有很多stackoverflow线程,但是我仍然 没有找到一个好的解决方案。 如果该应用程序不在后台,则可以签 launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]入 application:didFinishLaunchingWithOptions:呼叫以查看是否已从 通知中打开该应用程序。 如果应用