Android 8.1 App开机自启动、注册为无障碍服务、实现悬浮窗

     (欢迎转载,只需注明本文来源:https://blog.csdn.net/actionwind/article/details/103619688)

      以下各方法大多来自于网上诸多朋友的无私分享,但找到的方法多多少少都有些问题,所以抄抄改改最终都成功实现,综合一下发出来:

App开机自启动:

    一、AndroidManifest:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<receiver
    android:name=".BootReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter android:priority="1000">
        <action android:name="android.intent.action.BOOT_COMPLETED"></action>
        <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>

     二、代码:

//实现自启动功能

package lock.guardian;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BootReceiver  extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
                Intent thisIntent = new Intent(context, Guardian.class);//设置要启动的app
                thisIntent.setAction("android.intent.action.MAIN");
                thisIntent.addCategory("android.intent.category.LAUNCHER");
                thisIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(thisIntent);
            }
        }
    }

三、注意手机上的自启动管理功能,要将你的app加入白名单。

注册成为无障碍服务:

一、AndroidManifest:

<service
    android:name="LockGuardianService"
    android:process="system"
    android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
    <!--android:label="@string/accessibility_service_label"-->
    <intent-filter>
        <action android:name="android.accessibilityservice.AccessibilityService" />
    </intent-filter>
</service>

实现悬浮窗:

一、AndroidManifest:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW"/>

二、代码:

private void showFloatingWindow() {
    if (Settings.canDrawOverlays(this)) {
        // 获取WindowManager服务
        WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        // 新建悬浮窗控件
        Button button = new Button(getApplicationContext());
        //button.setText("Floating Window");
        button.setBackgroundColor(Color.GREEN);
        button.setAlpha(0.1f);

        // 根据android版本设置悬浮窗类型
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;            
            layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
        } else {
            layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        
        layoutParams.format = PixelFormat.RGBA_8888;

        //设置大小
        layoutParams.width = 140;
        layoutParams.height = 140;
        
        //设置位置
        layoutParams.x = 0;
        layoutParams.y = 788;

        // 将悬浮窗控件添加到WindowManager
        windowManager.addView(button, layoutParams);
    }
}

扩展阅读,此方法还未试验:《Android 8.1 为自己的APK的服务默认开启无障碍权限,不会有任何的弹窗》
https://blog.csdn.net/qq_28837389/article/details/98866171

扫描二维码关注公众号,回复: 8772364 查看本文章
发布了17 篇原创文章 · 获赞 4 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/actionwind/article/details/103619688