代码路径frameworks/base/service/core/com/android/server/policy/PhoneWindowManager.java
1、 PhoneWindowManager.java中关于根据包名实现悬浮窗权限授权的功能实现
在实现根据包名授予悬浮窗权限的核心的功能开发中,在通过上述的功能原理实现的过程中分析得知,在系统核心服务启动完毕以后,就需要在
PhoneWindowManager.java中的systemReady()中来添加默认授权的相关方法,接下来看下具体的实现过程
```/** {@inheritDoc} */@Overridepublic void systemReady() {// In normal flow, systemReady is called before other system services are ready.// So it is better not to bind keyguard here.mKeyguardDelegate.onSystemReady();mVrManagerInternal = LocalServices.getService(VrManagerInternal.class);if (mVrManagerInternal != null) {mVrManagerInternal.addPersistentVrModeStateListener(mPersistentVrModeListener);}readCameraLensCoverState();updateUiMode();mDefaultDisplayRotation.updateOrientationListener();synchronized (mLock) {mSystemReady = true;mHandler.post(new Runnable() {@Overridepublic void run() {updateSettings();}});// If this happens, for whatever reason, systemReady came later than systemBooted.// And keyguard should be already bound from systemBootedif (mSystemBooted) {mKeyguardDelegate.onBootCompleted();}}mAutofillManagerInternal = LocalServices.getService(AutofillManagerInternal.class);mGestureLauncherService = LocalServices.getService(GestureLauncherService.class);+ allowAppSystemAlertWindowPermission("包名") ;}
```
2、在实现根据包名授予悬浮窗权限的核心的功能开发中,可以在PhoneWindowManager这里增加授权悬浮窗权限的方法,来实现授权悬浮窗的功能,具体实现如下
增加授权悬浮窗的方法如下:
```public void allowAppSystemAlertWindowPermission(String pkg) throws RemoteException {final long ident = Binder.clearCallingIdentity();try {if (!TextUtils.isEmpty(pkg)) {Log.e(TAG, "allowAppSystemAlertWindowPermission pkg:" + pkg);AppOpsManager mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);PackageManager packageManager = mContext.getPackageManager();ApplicationInfo applicationInfo = packageManager.getApplicationInfo(pkg, PackageManager.GET_ACTIVITIES);mAppOpsManager.setMode(AppOpsManager.OP_SYSTEM_ALERT_WINDOW, applicationInfo.uid, pkg, AppOpsManager.MODE_ALLOWED);}} catch (PackageManager.NameNotFoundException e) {e.printStackTrace();} finally {Binder.restoreCallingIdentity(ident);}}```
3、如果你想要增加MDM系统控制接口也可以,增加一个aidl接口,然后执行make update-api 更新aidl接口(不知道增加aidl接口或者不知道添加源码路径可以参考我之前这篇文章高通 Android 12 源码编译aidl接口_安卓12 怎么写aidl-CSDN博客),然后在对应自定义系统服务中调用allowAppSystemAlertWindowPermission方法,也可以实现此功能,同样第三方应用SampleCode去调用自定义系统服务方法,也可以实现授权悬浮窗权限哈。需要系统签名哈!
android:sharedUserId="android.uid.system"
4、在app Sample应用申请此悬浮窗权限,只能通过判断当前sdk版本号如下代码所示,但是需要在 AndroidManifest.xml 中添加悬浮窗权限 如下所示。
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
其他伪代码如下所示,仅供参考哈
package xxx;import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;import androidx.appcompat.app.AppCompatActivity;public class MainActivity extends AppCompatActivity {private static final int REQUEST_CODE_OVERLAY_PERMISSION = 200;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {if (!Settings.canDrawOverlays(this)) {// 权限未授予,需要请求权限Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,Uri.parse("package:" + getPackageName()));startActivityForResult(intent, REQUEST_CODE_OVERLAY_PERMISSION);} else {// 权限已经被授予,执行相应操作}}
}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (requestCode == REQUEST_CODE_OVERLAY_PERMISSION) {if (Settings.canDrawOverlays(this)) {// 权限已经被用户授予,可以执行需要的操作} else {// 用户拒绝了权限请求,可以相应地处理}}}
}
这里只是简单记录下,转载请注明出处高通Android 12 /13根据包名授权悬浮窗权限-CSDN博客,谢谢!