Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 通知使用權限的實現原理剖析

編輯:關於 Android 程式設計

通知使用權限的打開路徑

設置 —— 提示音和通知 —— 通知使用權。

界面詳情如下:

存在需要通知使用權的應用時:

不存在這類應用時:

用戶勾選某個應用的復選框後,系統會彈出對話框要求二次確認:

涉及的主要文件

/packages/apps/Settings/src/com/android/settings/notification/NotificationAccessSettings.java

/packages/apps/Settings/src/com/android/settings/notification/ManagedServiceSettings.java

涉及的數據庫

data/data/com.android.providers.settings

詳細講解

先看 NotificationAccessSettings.java:

 public class NotificationAccessSettings extends ManagedServiceSettings { private static final String TAG = NotificationAccessSettings.class.getSimpleName(); private static final Config CONFIG = getNotificationListenerConfig(); private static Config getNotificationListenerConfig() { final Config c = new Config(); c.tag = TAG; /* Settings.Secure.ENABLED_NOTIFICATION_LISTENERS 是數據庫字段,通過讀取它判斷應用是否有通知使用權,有則界面上對應 checkbox 為勾選狀態;用戶勾選後會通過此字段向數據庫寫入應用信息。 */ c.setting = Settings.Secure.ENABLED_NOTIFICATION_LISTENERS; c.intentAction = NotificationListenerService.SERVICE_INTERFACE; /* 應用必須在 manifest 中聲明這個服務權限,才會被檢測到並顯示在通知使用權界面 */ c.permission = android.Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE; c.noun = "notification listener"; c.warningDialogTitle = R.string.notification_listener_security_warning_title; c.warningDialogSummary = R.string.notification_listener_security_warning_summary; c.emptyText = R.string.no_notification_listeners; return c; } @Override protected Config getConfig() { return CONFIG; } }

再看作為抽象基類的 ManagedServiceSettings.java,它承擔了列表加載、勾選交互與數據讀寫的核心邏輯:

 public abstract class ManagedServiceSettings extends ListFragment { private final Config mConfig; private PackageManager mPM; private ContentResolver mCR; private final HashSet mEnabledServices = new HashSet(); abstract protected Config getConfig(); private final ContentObserver mSettingsObserver = new ContentObserver(new Handler()) { @Override public void onChange(boolean selfChange, Uri uri) { updateList(); } }; /* 監聽應用增減變化時刷新列表 */ private final BroadcastReceiver mPackageReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { updateList(); } }; /* 用戶勾選後彈出確認對話框 */ public class ScaryWarningDialogFragment extends DialogFragment { public Dialog onCreateDialog(Bundle savedInstanceState) { /* ... */ return new AlertDialog.Builder(getActivity()) .setMessage(summary) .setTitle(title) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { mEnabledServices.add(cn); saveEnabledServices(); // 數據庫寫操作 } }) .setNegativeButton(android.R.string.cancel, null) .create(); } } /* 從數據庫加載已授權應用 */ private void loadEnabledServices() { mEnabledServices.clear(); final String flat = Settings.Secure.getString(mCR, mConfig.setting); if (flat != null && !"".equals(flat)) { final String[] names = flat.split(":"); for (int i = 0; i < names.length; i++) { final ComponentName cn = ComponentName.unflattenFromString(names[i]); if (cn != null) mEnabledServices.add(cn); } } } /* 數據庫寫操作 */ private void saveEnabledServices() { StringBuilder sb = null; for (ComponentName cn : mEnabledServices) { if (sb == null) sb = new StringBuilder(); else sb.append(':'); sb.append(cn.flattenToString()); } Settings.Secure.putString(mCR, mConfig.setting, sb != null ? sb.toString() : ""); } /* 更新列表 */ private void updateList() { loadEnabledServices(); getServices(mConfig, mListAdapter, mPM); mListAdapter.sort(new PackageItemInfo.DisplayNameComparator(mPM)); getListView().setAdapter(mListAdapter); } @Override public void onListItemClick(ListView l, View v, int position, long id) { ServiceInfo info = mListAdapter.getItem(position); final ComponentName cn = new ComponentName(info.packageName, info.name); if (mEnabledServices.contains(cn)) { mEnabledServices.remove(cn); saveEnabledServices(); } else { new ScaryWarningDialogFragment() .setServiceInfo(cn, info.loadLabel(mPM).toString()) .show(getFragmentManager(), "dialog"); } } }

整個流程可以這樣理解:NotificationAccessSettings 通過 Config 描述「通知使用權」這一具體功能的各項參數(數據庫字段、intentAction、權限、對話框文案等);ManagedServiceSettings 則提供通用框架,負責掃描系統裡所有符合條件的服務、渲染列表、處理勾選並讀寫數據庫。

具體來說:getServices 通過 queryIntentServicesAsUser 查詢所有匹配 SERVICE_INTERFACE 的服務,並校驗是否具備 BIND_NOTIFICATION_LISTENER_SERVICE 權限,具備才會加入列表;loadEnabledServices 從 Settings.Secure 中讀出已授權的組件;saveEnabledServices 把勾選結果寫回數據庫;用戶點擊列表項時,若已授權則直接移除,否則彈出 ScaryWarningDialogFragment 做安全提示,確認後才寫入。

數據庫相關信息

數據庫字段中,每個應用信息以「包名/service」的形式存儲,多個應用之間用「:」分隔。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved