gpt4 book ai didi

android - 检测键盘在 android 上的应用程序之外处于 Activity 状态

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:54:38 27 4
gpt4 key购买 nike

我正在尝试使用无障碍服务检测键盘是否处于 Activity 状态(在我的应用程序之外)。为此,我尝试阅读通知“选择键盘”(启用多个键盘时)。使用以下代码。

public class KeyboardWatcher extends AccessibilityService {

boolean isConnected = false;
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
final String packagename = String.valueOf(event.getPackageName());
Log.d("Package", packagename);
String msg = "";
List<CharSequence> s = event.getText();
if(s.iterator().hasNext())
{
msg += s.iterator().next().toString();
Log.d("MSG", msg);

}else{
Log.d("TYPE", event.getEventType()+"");
}
}else{
Log.d("EVENT TYPE__",event.getEventType()+"");
final String packagename = String.valueOf(event.getPackageName());
Log.d("PNE", packagename);
}

}
protected void onServiceConnected() {

if (isConnected) {
return;
}
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
setServiceInfo(info);
isConnected = true;
}
}

现在,除“键盘选择器”通知外,所有通知均由应用程序记录。如何阅读该通知,是否可能。

谢谢

最佳答案

package com.sat.app.notification;
import android.content.Context;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
public class NotificationService extends NotificationListenerService {
Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
String pack = sbn.getPackageName();
String title = sbn.getNotification().extras.getString("android.title");// It will be select keyboard
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.i("com.sat","Notification Removed");

}
}

在 list 中:添加

<service android:name="com.sat.app.notification.NotificationService"
android:label="@string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>

安装应用程序后允许在设置中阅读通知。经过测试并在 Android 5.0 上运行

关于android - 检测键盘在 android 上的应用程序之外处于 Activity 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27467570/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com