简体   繁体   English

如何获得前台的活动窗口?

[英]How do I get active window that is on foreground?

my Q is pretty straightforward.我的 Q 很简单。

I can't figure the way to modify properties of the currently active window from my service.我无法找到从我的服务修改当前活动窗口属性的方法。 I saw some threads here on stack, also elsewhere, but none of them really answers.我在堆栈上看到了一些线程,也在其他地方看到了一些线程,但没有一个真正回答。 The foreground (active) activity can be just anything.前台(活动)活动可以是任何东西。 I have no access to it.我无权访问它。

This guys seems to ask same Q, getting no A... How to get the foreground activity instance?这家伙似乎问同样的问题,没有得到 A... 如何获得前台活动实例?

Many thanx in advance !提前多谢!

Unfortunatelly Android seems not to support such demand, to modify window of top app, from service running on background.不幸的是,Android 似乎不支持这样的需求,从后台运行的服务修改顶级应用程序的窗口。

I found this as the best appropriate do far:我发现这是最合适的方法:

http://developer.android.com/guide/components/services.html http://developer.android.com/guide/components/services.html

" Sending Notifications to the User Once running, a service can notify the user of events using Toast Notifications or Status Bar Notifications. " 向用户发送通知 一旦运行,服务可以使用 Toast 通知或状态栏通知通知用户事件。

A toast notification is a message that appears on the surface of the current window for a moment then disappears, while a status bar notification provides an icon in the status bar with a message, which the user can select in order to take an action (such as start an activity). Toast 通知是在当前窗口表面出现片刻然后消失的消息,而状态栏通知在状态栏中提供一个带有消息的图标,用户可以选择该图标以执行操作(例如作为开始活动)。

Usually, a status bar notification is the best technique when some background work has completed (such as a file completed downloading) and the user can now act on it.通常,当某些后台工作已完成(例如文件已完成下载)并且用户现在可以对其进行操作时,状态栏通知是最佳技术。 When the user selects the notification from the expanded view, the notification can start an activity (such as to view the downloaded file).当用户从展开的视图中选择通知时,通知可以启动一个活动(例如查看下载的文件)。

See the Toast Notifications or Status Bar Notifications developer guides for more information."有关更多信息,请参阅 Toast 通知或状态栏通知开发人员指南。”

Accessibility services provide a means to interact with the foregroung Service.无障碍服务提供了一种与前台服务交互的方式。 It is kind of like a workaround but still effective.这有点像一种解决方法,但仍然有效。

To make use of this, extend the Accessibility Service and create a new class.要利用这一点,请扩展 Accessibility Service 并创建一个新类。 Listen for AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED event.侦听 AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED 事件。 This event is triggered whenever the foreground activity is changed.每当前台活动发生更改时都会触发此事件。 The current foreground process name is available from the event object.当前前台进程名称可从事件对象中获得。

AccessibilityService.java无障碍服务.java

public class AccessibilityService extends android.accessibilityservice.AccessibilityService{
    public static AccessibilityService instance;
    @Override
    public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
        // Note : This event is sometimes called more than one for a foreground service
        if (accessibilityEvent.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED){
            Log.d("Event","TYPE_WINDOW_STATE_CHANGED");
            Log.d("Pkg",accessibilityEvent.getPackageName().toString());

            // Check PackageName matching here and continue with code
            // Here we prevent whatsapp from opening
            // Each time it launches we simulate the press of back button
            if (accessibilityEvent.getPackageName().equals("com.whatsapp")){
                doAction();
            }

        }

    }

    @Override
    public void onInterrupt() {

    }

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        Log.d("Accessibility","Service Connected");
    }

    public void doAction(){
        performGlobalAction(GLOBAL_ACTION_BACK);
    }
}

Here I check if the foreground service is Whatsapp and then continue with my code if it is.在这里,我检查前台服务是否是 Whatsapp,如果是,则继续我的代码。

In addition to this class, this service has to be declared in the manifest file in the application section along with a config file in the res/xml folder除了这个类之外,这个服务还必须在应用程序部分的清单文件中与 res/xml 文件夹中的配置文件一起声明

<service android:name=".AccessibilityService"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService" />
            </intent-filter>
            <meta-data
            android:name="android.accessibilityservice"
            android:resource="@xml/serviceconfig"/>
</service>



serviceconfig.xml服务配置文件

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeAllMask"
    android:accessibilityFeedbackType="feedbackAllMask"
    android:accessibilityFlags="flagDefault"
    android:notificationTimeout="100"
    android:canRetrieveWindowContent="true"
    />


Also ensure accessibility services permission is allowed by the user.还要确保用户允许无障碍服务许可。
The complete code for sample app to log the foreground process name is available at https://github.com/abinpaul1/Android-Snippets/tree/master/GetForegroundService用于记录前台进程名称的示例应用程序的完整代码可在https://github.com/abinpaul1/Android-Snippets/tree/master/GetForegroundService 获得

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM