gpt4 book ai didi

android - 如何使用无障碍服务?

转载 作者:太空狗 更新时间:2023-10-29 16:17:07 25 4
gpt4 key购买 nike

我正在研究 AccessibilityService。我研究了什么是无障碍服务以及我们为什么使用它,无障碍服务有什么优势。所以我尝试制作一个辅助功能服务的演示,看看它是如何工作的,当电子邮件或任何 whatsapp,微信消息出现时,它会以哪种方式发送,但我没有成功。当我开始这个项目时,我在 StartService(i) 线上得到了 Exception;因此,请仔细查看这段代码并指出我犯了什么错误,您的评论和建议对我和 AccessibilityService 初学者都非常有帮助。

我使用的代码

在 MainActivity 中...

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = new Intent(MainActivity.this, MyService.class);
startService(i);
}
}

在 AccessibilityService 类中...

public class MyService extends AccessibilityService{


@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
// TODO Auto-generated method stub
Log.i("Accessibility", "onAccessibilityEvent");
Toast.makeText(getApplicationContext(), "onAccessibilityEvent", Toast.LENGTH_SHORT).show();
}

@Override
public void onInterrupt() {
// TODO Auto-generated method stub
Log.i("Interrupt", "Interrupt");
Toast.makeText(getApplicationContext(), "onInterrupt", Toast.LENGTH_SHORT).show();

}

@Override
protected void onServiceConnected() {
// TODO Auto-generated method stub
super.onServiceConnected();
Log.i("Service", "Connected");
Toast.makeText(getApplicationContext(), "onServiceConnected", Toast.LENGTH_SHORT).show();
}

}

在 list 文件中...

<uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.notifications.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service
android:name="MyService"
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/accessibilityservice" />
</service>

</application>

在 res/xml 文件夹中,即 accessibilityservice.xml 文件...

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

我得到的logcat...

06-29 01:58:40.798: E/AndroidRuntime(21707): FATAL EXCEPTION: main
06-29 01:58:40.798: E/AndroidRuntime(21707): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.notifications/com.example.notifications.MainActivity}: java.lang.SecurityException: Not allowed to start service Intent { cmp=com.example.notifications/.MyService } without permission android.permission.BIND_ACCESSIBILITY_SERVICE
06-29 01:58:40.798: E/AndroidRuntime(21707): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
06-29 01:58:40.798: E/AndroidRuntime(21707): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
06-29 01:58:40.798: E/AndroidRuntime(21707): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
06-29 01:58:40.798: E/AndroidRuntime(21707): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
06-29 01:58:40.798: E/AndroidRuntime(21707): at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 01:58:40.798: E/AndroidRuntime(21707): at android.os.Looper.loop(Looper.java:130)
06-29 01:58:40.798: E/AndroidRuntime(21707): at android.app.ActivityThread.main(ActivityThread.java:3701)
06-29 01:58:40.798: E/AndroidRuntime(21707): at java.lang.reflect.Method.invokeNative(Native Method)
06-29 01:58:40.798: E/AndroidRuntime(21707): at java.lang.reflect.Method.invoke(Method.java:507)
06-29 01:58:40.798: E/AndroidRuntime(21707): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
06-29 01:58:40.798: E/AndroidRuntime(21707): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
06-29 01:58:40.798: E/AndroidRuntime(21707): at dalvik.system.NativeStart.main(Native Method)
06-29 01:58:40.798: E/AndroidRuntime(21707): Caused by: java.lang.SecurityException: Not allowed to start service Intent { cmp=com.example.notifications/.MyService } without permission android.permission.BIND_ACCESSIBILITY_SERVICE
06-29 01:58:40.798: E/AndroidRuntime(21707): at android.app.ContextImpl.startService(ContextImpl.java:867)
06-29 01:58:40.798: E/AndroidRuntime(21707): at android.content.ContextWrapper.startService(ContextWrapper.java:336)
06-29 01:58:40.798: E/AndroidRuntime(21707): at com.example.notifications.MainActivity.onCreate(MainActivity.java:15)
06-29 01:58:40.798: E/AndroidRuntime(21707): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-29 01:58:40.798: E/AndroidRuntime(21707): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
06-29 01:58:40.798: E/AndroidRuntime(21707): ... 11 more

最佳答案

您不能“启动”AccessibilityService,因为它们与您可以控制其启动/停止的其他服务不同。在这里,Android 系统根据用户选择的设置来控制它们的启动和停止。

您最好的办法是启动辅助功能设置页面并让用户为您启用它:

Intent openSettings = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
openSettings.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(openSettings);

希望对您有所帮助!

关于android - 如何使用无障碍服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24470647/

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