? ? 代碼路徑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博客,謝謝!?