Android 8.1 App开机自启动、注册为无障碍服务、实现悬浮窗 |
您所在的位置:网站首页 › 一加设置悬浮窗权限 › Android 8.1 App开机自启动、注册为无障碍服务、实现悬浮窗 |
(欢迎转载,只需注明本文来源:https://blog.csdn.net/actionwind/article/details/103619688) 以下各方法大多来自于网上诸多朋友的无私分享,但找到的方法多多少少都有些问题,所以抄抄改改最终都成功实现,综合一下发出来: App开机自启动:一、AndroidManifest: 二、代码: //实现自启动功能 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加入白名单。 注册成为无障碍服务:(Android无障碍服务开发QQ群:752296312)一、AndroidManifest: 实现悬浮窗:一、AndroidManifest: 二、代码: 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); } }三、很容易忽略的一点:在手机上设置这个app启用悬浮窗权限。 用代码判断的方法是在onCreate方法中加入: //在启动服务之前,需要先判断一下当前是否允许开启悬浮窗,若无则发出提示并跳转到手动授权页面 if (!Settings.canDrawOverlays(this)) { Toast.makeText(this, "当前无开启悬浮窗权限,请授权", Toast.LENGTH_SHORT); startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())), 0); }扩展阅读,此方法还未试验:《Android 8.1 为自己的APK的服务默认开启无障碍权限,不会有任何的弹窗》RK3326 Android 8.1 为自己的APK的服务默认开启无障碍权限,不会有任何的弹窗_浅尝辄止RELAX的博客-CSDN博客 |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |