Android保活无障碍服务(辅助功能)
填写昵称已被占用
编辑于 2021年07月18日 12:19

这篇文章是在前篇文章的基础上编写的,建议先看网页链接​

阅读本文章前需要对无障碍服务有所了解!


1、创建自己的无障碍服务类

代码块
JavaScript
自动换行
复制代码
public class MyAccessibility extends AccessibilityService {
    protected void onServiceConnected() {
        //服务启动时    
    }
    public void onAccessibilityEvent(AccessibilityEvent enent) {
        //辅助功能事件监听 及 运行    
    }
    public void onInterrupt() {
        //辅助功能中断时
    }
}
复制成功

2、编写前台通知

写法与前篇文章 Android前台服务保活app​ 中保持一致,不再过多解释

代码块
JavaScript
自动换行
复制代码
    //创建前台通知,可写成方法体,也可单独写成一个类
    private Notification createForegroundNotification(){
        //前台通知的id名,任意
        String channelId = "ForegroundService";
        //前台通知的名称,任意
        String channelName = "Service";
        //发送通知的等级,此处为高,根据业务情况而定
        int importance = NotificationManager.IMPORTANCE_HIGH;
        //判断Android版本,不同的Android版本请求不一样,以下代码为官方写法
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            NotificationChannel channel = new NotificationChannel(channelId,channelName,importance);
            channel.setLightColor(Color.BLUE);
            channel.setLockscreenVisiability(Notification.VISIBILITY_PRIVATE);
            NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(channel);
        }

        //点击通知时可进入的Activity
        Intent notificationIntent = new Intent(this,MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,notificationIntent,0);

        //最终创建的通知,以下代码为官方写法
        //注释部分是可扩展的参数,根据自己的功能需求添加
        return new NotificationCompat.Builder(this,channelId)
        	 .setContentTitle("填写通知的标题")
          	 .setContentText("填写通知的内容")
            .setSmallIcon(Icon)//通知显示的图标
            .setContentIntent(pendingIntent)//点击通知进入Activity
            .setTicker("通知的提示语")
            .build();
            //.setOngoing(true)
            //.setPriority(NotificationCompat.PRIORITY_MAX)
            //.setCategory(Notification.CATEGORY_TRANSPORT)
            //.setLargeIcon(Icon)
            //.setWhen(System.currentTimeMillis())
    }
复制成功

3、重写onCreate() 和onDestroy() 

写法与前篇文章 Android前台服务保活app​ 中保持一致,不再过多解释

代码块
JavaScript
自动换行
复制代码
    //服务创建时
    @Override
    public void onCreate(){
        super.onCreate();
        //服务创建时创建前台通知
        Notification notification = createForegroundNotification();
        //启动前台服务
        startForeground(1,notification);
    }

    //服务销毁时
    @Override
    public void onDestroy(){
        //在服务被销毁时,关闭前台服务
        stopForeground(true);
        super.onDestroy();
    }
复制成功

4、配置清单文件

唯一不同的就是加上 android:foregroundServiceType="location&#​34; 前台服务属性。这样算配置完成了。

代码块
JavaScript
自动换行
复制代码
<service
    android:name=".MyAccessibility"
    android:enabled="true"
    android:exported="true"
    android:foregroundServiceType="location"
    android:label="无障碍辅助功能名称"
    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/accessibility_config" />
</service>
复制成功

当启动无障碍服务时会自动启动前台服务,只要前台服务存在,无障碍就可以一直运行!如果没有加入前台服务,无障碍服务是运行在后台的,非常容易被系统杀死!!!(个人见解)

最后要记得设置电池优化,把你的app设置为无限制,开启应用自启动 (手机重启会自动开启无障碍服务和前台服务,让你的服务第一时间到达战场)


测试机器:小米8SE

Android版本:Android 10

测试结果:app在后台4天(未曾打开app到前台),无障碍服务依旧在运行

如果本篇文章对你有帮助,希望可以点赞+投币支持一下哦