安卓模拟器 模拟滑动事件_模拟网络事件

模拟Web事件
本文介绍了一个Python包,用于生成半随机的Web事件,这些事件遵循预定义的约束,使其看起来更真实。通过使用此包,您可以为数据分析工程师职位构建投资组合,演示创建Web会话、衡量转化率以及按广告系列或来源划分指标的能力。

安卓模拟器 模拟滑动事件

You want to implement a new streaming pipeline on your workplace and need to show your managers a Proof Of Concept. This POC should allow you to demonstrate some of the functionalities, in this case, generate real-time metrics. However, there is a limitation: you can’t use production data on a POC. How do you solve that?

您想要在工作场所实施新的流传输管道,并且需要向您的经理展示概念证明。 此POC应该使您能够演示一些功能,在这种情况下,可以生成实时指标。 但是,有一个限制:您不能在POC上使用生产数据。 您如何解决?

If your answer was to generate fake events, then you are right. It will probably be the best solution. Then you create some random events and push then to the pipeline. You’ll soon realize that random is not the same as fake. You certainly don’t want to show a chart like this to your managers:

如果您的答案是生成假事件,那么您是对的。 这可能是最好的解决方案。 然后创建一些随机事件,然后推送到管道。 您很快就会意识到,随机与伪造并不相同。 您当然不希望向经理显示这样的图表:

Random events don’t make a point; they don’t sell anything. Let’s have a grasp at another metric created on top of random events:

随机事件没有意义; 他们什么都不卖。 让我们了解在随机事件之上创建的另一个指标:

Image for post
Random page-views don’t make any sense. No one expects a website to have more visits on payment page than Home. Chart by the Author.
随机页面浏览没有任何意义。 没有人期望网站在支付页面上的访问比首页更多。 作者的图表。

Now let’s say you want to build your portfolio for an Analytics Engineer position. You possibly need to show that you are capable of creating web sessions, measuring conversion rate and splitting metrics by campaign or source. How do you build your damn portfolio if you don’t have data? And again, generating random data won't allow you to measure anything.

现在,假设您要为Analytics(分析)工程师职位建立投资组合。 您可能需要证明自己具有创建网络会话,衡量转化率以及按广告系列或来源划分指标的能力。 如果没有数据,如何建立该死的投资组合? 再说一次,生成随机数据将使您无法进行任何测量。

创建半随机事件 (Creating semi-random events)

Thinking about those problems, I decided to create a python package to generate fake events that are not entirely random. They follow a set of constraints and allow you to create beautiful things with fake data that looks like real. Look again at the page-views over time chart:

考虑到这些问题,我决定创建一个python包来生成并非完全随机的虚假事件。 它们遵循一系列约束,并允许您使用看起来像真实的假数据来创建漂亮的东西。 再次查看时间图表内的浏览量:

Image for post
Fake web events. Now we have more realistic page-views over time. Chart by the Author.
伪造的网络事件。 现在,随着时间的推移,我们将获得更逼真的页面浏览量。 作者的图表。

If we look at the page-views funnel, we would expect to see more visits on Home and much less on payment and confirmation, right? Let’s check if that is true.

如果我们查看综合浏览量渠道,那么我们期望看到更多的“首页”访问,而更少的付款和确认访问,对吗? 让我们检查一下是否正确。

Image for post
Fake web events. A funnel that makes sense! Chart by the Author.
伪造的网络事件。 一个有意义的漏斗! 作者的图表。

如何自行创建虚假的网络事件? (How to create fake web events on your own?)

Is quite simple! The package is available on PyPI and source code on GitHub. To install you do pip install, as you would for any other package:

很简单! 该软件包可在PyPI上获得源代码可在GitHub上获得 。 要进行安装 ,请像其他任何软件包一样进行pip install

pip install fake_web_events

It is also simple to use. See the example below that will print events to stdout.

使用也很简单。 请参见下面的示例,该示例会将事件打印到stdout。

from fake_web_events import Simulationsimulation = Simulation(user_pool_size=100, sessions_per_day=10000)
events = simulation.run(duration_seconds=10)for event in events:
print(event)

You can do anything you want inside the loop. Send it to Kafka or Kinesis. Process it with Spark streaming and create some metrics. Save to a Postgres and build some dashboards using Metabase. Show off your SQL skills on your portfolio. Or get that POC approved.

您可以在循环内做任何您想做的事情。 将其发送到Kafka或Kinesis。 使用Spark流处理它并创建一些指标。 保存到Postgres并使用元数据库构建一些仪表板。 在您的产品组合上炫耀您SQL技能。 或获得该POC批准。

An event created by this package will look like this:

此包创建的事件将如下所示:

{
"event_timestamp": "2020-07-05 14:32:45.407110",
"event_type": "pageview",
"page_url": "http://www.dummywebsite.com/home",
"page_url_path": "/home",
"referer_url": "www.instagram.com",
"referer_url_scheme": "http",
"referer_url_port": "80",
"referer_medium": "internal",
"utm_medium": "organic",
"utm_source": "instagram",
"utm_content": "ad_2",
"utm_campaign": "campaign_2",
"click_id": "b6b1a8ad-88ca-4fc7-b269-6c9efbbdad55",
"geo_latitude": "41.75338",
"geo_longitude": "-86.11084",
"geo_country": "US",
"geo_timezone": "America/Indiana/Indianapolis",
"geo_region_name": "Granger",
"ip_address": "209.139.207.244",
"browser_name": "Firefox",
"browser_user_agent": "Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_5_5; rv:1.9.6.20) Gecko/2012-06-06 09:24:19 Firefox/3.6.20",
"browser_language": "tn_ZA",
"os": "Android 2.0.1",
"os_name": "Android",
"os_timezone": "America/Indiana/Indianapolis",
"device_type": "Mobile",
"device_is_mobile": true,
"user_custom_id": "vsnyder@hotmail.com",
"user_domain_id": "3d648067-9088-4d7e-ad32-45d009e8246a"
}

Fake Web Events is using Faker to create some data; other fields are randomly generated based on predefined weights (that you can customize in a YAML file).

Fake Web Events正在使用Faker创建一些数据。 其他字段是根据预定义的权重(您可以在YAML文件中自定义)随机生成的。

虚假的网络事件 (Fake web events under the hood)

I assume you might be interested in understanding how those events are created and in what is the logic behind it. I can tell you straight away that it’s not Alien technology :).

我认为您可能对了解如何创建这些事件以及其背后的逻辑感兴趣。 我可以马上告诉您,这不是外星人技术:)。

u/Huesh on u / HueshReddit. Reddit上

用户数 (Users)

We start by creating a pool of users. Some fields are entirely random (coming from Faker), others follow a set of predefined weights. For example, the user will get a browser assigned using the following weights:

我们首先创建一个用户池。 一些字段是完全随机的(来自Faker),其他字段则遵循一组预定义的权重。 例如,用户将获得使用以下权重分配的浏览器:

browsers:
Chrome: 0.5
Firefox: 0.25
InternetExplorer: 0.05
Safari: 0.1
Opera: 0.1

Meaning that Chrome will appear on roughly 50% of users and Firefox on 25%.

这意味着Chrome将在大约50%的用户中显示,而Firefox在25%的用户中显示。

A user has essentially the same JSON structure than an event, except by the fact that it doesn’t contain timestamps or page information. When we run the simulation, users will be randomly picked from the pool to start “navigating the website”. Users might return to the site later, that’s why they are chosen with reposition.

用户具有与事件基本相同的JSON结构,但事实是它不包含时间戳或页面信息。 当我们运行模拟时,将从池中随机挑选用户以开始“浏览网站”。 用户可能稍后会返回该站点,这就是为什么选择重新定位他们的原因。

大事记 (Events)

To create events, we simulate sessions. There are a few “pages” created in the simulation. The user will start either at Home, Product A or Product B pages. At each iteration, the user could stay on the same page, move to another page, or finalize the session. Below are the probabilities for a user that is currently on Home:

为了创建事件,我们模拟会话。 在模拟中创建了一些“页面”。 用户将从首页,产品A或产品B页面开始。 在每次迭代时,用户可以停留在同一页面上,移至另一页面或完成会话。 以下是当前在家中的用户的概率:

home:
home: 0.45
product_a: 0.17
product_b: 0.12
session_end: 0.26

The simulation tracks all users and generates an event every time it reaches a new page. Once the user reaches Session End, his session is dropped. Follows a diagram of the possible user events during the simulation:

模拟会跟踪所有用户,并在每次到达新页面时生成一个事件。 用户到达会话结束后,其会话将被丢弃。 遵循模拟过程中可能的用户事件的图表:

Image for post
Site map for the fake web events simulation. Green pages are those where a user can land at the beginning of a session. Yellow pages are only accessible to users who are already browsing. Diagram by the Author.
假网站事件模拟的站点地图。 绿页是用户可以在会话开始时进入的页面。 黄页仅适用于已经浏览的用户。 由作者绘制的图。

模拟 (Simulation)

The simulation will run in batches, and its internal clock will usually go much faster than the standard time. The exception is if you are running it with very large user_pool_size and sessions_per_day.

模拟将分批运行,其内部时钟通常会比标准时间快得多。 唯一的例外是,如果你是非常大的user_pool_sizesessions_per_day运行它

The simulation will create a generator object. It won’t run or use resources until you start iterating over it.

模拟将创建一个生成器对象。 在开始对其进行迭代之前,它不会运行或使用资源。

接下来是什么? (What comes next?)

I do have a few ideas on what to do next on this project. Some are simple bug fixes or improved implementation of how events are generated and tracked. But there is one broad idea that I would like to explore more:

对于这个项目的下一步我确实有一些想法。 有些是简单的错误修复,或者是事件生成和跟踪的改进实现。 但是有一个广泛的想法,我想探索更多:

Producing microservices events on top of web events.

在Web事件之上产生微服务事件。

My idea is to add another module to the simulation that would create different events based on the microservices architecture. I can think of events like this:

我的想法是向模拟添加另一个模块,该模块将基于微服务架构创建不同的事件。 我可以想到这样的事件:

  • user_created

    user_created
  • order_created

    order_created
  • payment_completed

    付款完成
  • payment_failed

    支付失败
  • order_dispatched

    order_Dispatched
  • order_completed

    order_completed

Microservices events would be useful to create fake transactional databases that are bonded to web events. I think it would be useful for a lot of people as it would allow even more use cases.

微服务事件对于创建绑定到Web事件的虚假事务数据库很有用。 我认为这将对很多人有用,因为它将允许更多用例。

If you like this project, please show your love by starring it on GitHub.

如果您喜欢这个项目,请在GitHub上加注您的爱意。

Everyone can also contribute: create some issues, feature requests, or even Pull Requests if you are comfortable in touching the code!

每个人都可以做出贡献 :创建一些问题,功能请求,甚至是拉动请求(如果您习惯于触摸代码)!

翻译自: https://towardsdatascience.com/simulating-web-events-7199bf8afcfd

安卓模拟器 模拟滑动事件

<think>好的,用户现在想了解如何在Android中通过无障碍服务模拟触摸事件并打开相关功能。我需要结合之前的回答和用户提供的引用内容来给出详细的步骤。 首先,用户之前的问题是关于如何启用无障碍服务和实现控屏操作,我已经给出了启用步骤和代码示例。现在用户进一步询问模拟触摸事件的具体方法和如何打开相关功能。需要明确模拟触摸事件的不同方法,比如坐标点击和节点点击,以及如何正确配置无障碍服务。 用户提供的引用内容提到了几个关键点:引用1中的代码展示了如何检查无障碍服务是否开启,并在未开启时跳转到设置页面。引用2提到了在Android 11上使用dispatchGesture时遇到的问题及解决方法,这可能涉及到手势模拟的兼容性问题。引用3则介绍了一个开源仓库,其中包含模拟点击和节点交互的实现,这可能对用户有帮助。 接下来,我需要整理模拟触摸事件的具体步骤,包括服务配置、权限声明、实现点击逻辑(坐标和节点两种方式),以及如何确保服务保活和兼容性处理。同时,要引用用户提供的引用来支持回答,比如在服务保活部分提到使用instance变量来保持服务实例,以及在处理Android 11的问题时参考引用2的解决方案。 还要注意用户提到的“打开相关功能”,这可能涉及如何启动无障碍服务并保持其运行,因此需要包括检查服务状态并引导用户开启的代码示例,如引用1中的checkAccessibility方法。 最后,需要生成相关问题,涵盖用户可能感兴趣的后续问题,比如性能优化、多手势模拟、不同Android版本的适配等。确保回答结构清晰,步骤详细,并正确引用提供的参考资料中的内容。</think>#### 如何在 Android 中通过无障碍服务模拟触摸事件并打开功能 --- ### 一、核心实现步骤 #### 1. **服务配置与声明** - **声明无障碍服务权限** 在 `AndroidManifest.xml` 中添加以下配置: ```xml <service android:name=".MyTouchService" android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" android:label="触摸模拟服务"> <intent-filter> <action android:name="android.accessibilityservice.AccessibilityService" /> </intent-filter> <meta-data android:name="android.accessibilityservice" android:resource="@xml/touch_service_config" /> </service> ``` - **定义服务配置文件** 创建 `res/xml/touch_service_config.xml`: ```xml <accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" android:description="模拟触摸事件" android:accessibilityEventTypes="typeAllMask" android:accessibilityFeedbackType="feedbackGeneric" android:canPerformGestures="true" <!-- 关键:允许手势模拟 --> android:canRetrieveWindowContent="true" /> ``` --- #### 2. **模拟触摸事件方法** ##### **方法一:通过坐标模拟点击**(适用于固定位置操作) ```kotlin fun simulateTap(x: Int, y: Int) { val path = Path().apply { moveTo(x.toFloat(), y.toFloat()) } val gesture = GestureDescription.Builder() .addStroke( GestureDescription.StrokeDescription( path, 0, // 开始时间(毫秒) 50 // 持续时间(毫秒) ) ) .build() // 分派手势并监听结果 dispatchGesture(gesture, object : GestureResultCallback() { override fun onCompleted(gestureDescription: GestureDescription) { Log.d("Touch", "点击成功") } override fun onCancelled(gestureDescription: GestureDescription) { Log.e("Touch", "手势被取消(可能权限或设备限制)") // 引用[2]中的兼容性问题 } }, null) } ``` ##### **方法二:通过控件节点模拟点击**(动态适配界面) ```kotlin override fun onAccessibilityEvent(event: AccessibilityEvent) { val rootNode = rootInActiveWindow // 根据文本或ID查找按钮 val targetNode = rootNode.findAccessibilityNodeInfosByText("打开").firstOrNull() targetNode?.let { if (it.isClickable) { it.performAction(AccessibilityNodeInfo.ACTION_CLICK) Log.d("NodeClick", "成功触发点击") } } } ``` --- #### 3. **服务保活与初始化** - **服务实例管理** 引用[1]的单例模式确保服务存活: ```kotlin class MyTouchService : AccessibilityService() { companion object { var instance: MyTouchService? = null } override fun onServiceConnected() { instance = this Log.i("Service", "无障碍服务已启动") } override fun onDestroy() { instance = null super.onDestroy() } } ``` - **引导用户开启服务** 在 Activity 中调用检查逻辑: ```kotlin fun checkAndEnableService(context: Context) { if (MyTouchService.instance == null) { try { // 跳转无障碍设置页 context.startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)) Toast.makeText(context, "请开启触摸模拟服务", Toast.LENGTH_LONG).show() } catch (e: Exception) { context.startActivity(Intent(Settings.ACTION_SETTINGS)) // 兼容低版本设备 } } } ``` --- ### 二、兼容性处理(重点) 1. **Android 11+ 多手势问题** - 若遇到 `onCancelled` 回调,需检查设备是否支持触控屏幕(如模拟器可能返回 `isTouchableDisplay = false`)。 - 引用[2]的解决方案:修改系统源码判断逻辑(需 root 或定制 ROM),或改用单点触控。 2. **分辨率适配** - 使用百分比坐标而非绝对值: ```kotlin val screenWidth = Resources.getSystem().displayMetrics.widthPixels val screenHeight = Resources.getSystem().displayMetrics.heightPixels val targetX = (screenWidth * 0.5).toInt() // 屏幕横向中点 val targetY = (screenHeight * 0.8).toInt() // 屏幕纵向80%位置 ``` --- ### 三、应用场景示例 1. **自动化打开应用** ```kotlin fun openApp(packageName: String) { val intent = packageManager.getLaunchIntentForPackage(packageName) if (intent != null) { startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) } else { Log.e("App", "应用未安装") } } ``` 2. **模拟滑动解锁** ```kotlin fun simulateSwipe(startX: Int, startY: Int, endX: Int, endY: Int) { val path = Path().apply { moveTo(startX.toFloat(), startY.toFloat()) lineTo(endX.toFloat(), endY.toFloat()) } val gesture = GestureDescription.Builder() .addStroke(GestureDescription.StrokeDescription(path, 0, 500)) .build() dispatchGesture(gesture, null, null) } ``` --- ### 四、注意事项 - **权限与隐私** - 需在应用说明中明确告知用户服务用途(如引用[3]的合规性要求)。 - 禁止后台隐蔽操作(Android 10+ 会限制后台启动服务)。 - **性能优化** - 避免高频触发事件(可能导致 ANR)。 - 使用 `Handler.postDelayed` 控制操作间隔。 --- ### 相关问题 1. **如何解决 Android 11 上多手势模拟失败的问题?** - 答案需参考引用[2]的触控屏幕检测逻辑修改方案。 2. **无障碍服务在息屏状态下能否继续工作?** - 受系统省电策略限制,需申请 `WAKE_LOCK` 权限并保持前台服务。 3. **如何通过无障碍服务实现长按操作?** - 设置手势持续时间为 500ms 以上,例如: ```kotlin GestureDescription.StrokeDescription(path, 0, 800) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值