編輯:關於 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」的形式存儲,多個應用之間用「:」分隔。
微信電腦版怎麼看以前的聊天記錄?iPhone 資料還原圖文教學
電腦版微信比手機方便得多,打字快、看圖清楚,很多上班族幾乎整天掛在電腦上。但當你想在電腦上翻幾個月前的對話,卻發現手機端沒同步——這時候就得從手機的備份資料裡把它撈出來。
Android Gradle file crunching failed 問題解法
在 Gradle 建置过程中,若出現類似 Some file crunching failed 的錯誤,通常與資源圖片處理有關。常見訊息會指出某個圖片檔案在 crunch
Android 自定義支付密碼軟鍵盤實作(含隨機鍵盤)
Android 自定義輸入支付密碼的軟鍵盤 專案需要實現一個類似支付寶那樣的密碼鎖功能,帶有自己的軟鍵盤。這裡整理了相關資料供大家參考,若有錯誤歡迎留言指正。 需求:實現
用 Smalidea + IntelliJ IDEA / Android Studio 動態除錯 smali 程式碼
關於 smalidea smalidea 是 IntelliJ IDEA / Android Studio 的 smali 語言外掛,裝上之後就能像除錯 Java 一樣,