2014年8月31日日曜日

有効になっているNotificationListenerServiceの一覧を取得する

自分のNotificationListenerServiceが有効になっていなかったら有効になるようにユーザに促したいけどやりかたがよくわからず、ソースを読みました
忘れそうなのでメモっておく
ContentResolver contentResolver = context.getContentResolver();
String rawListeners = Settings.Secure.getString(contentResolver,
        "enabled_notification_listeners");
if (rawListeners == null || "".equals(rawListeners)) {
    return null;
}

String[] listeners = rawListeners.split(":");

//Stringだと中身の予想がつきにくいのでComponentNameに
ArrayList componentNames = new ArrayList();
for (String listener : listeners) {
    LogUtil.d(TAG,"enabled listener : "+listener);
    ComponentName cn = ComponentName.unflattenFromString(listener);
    if (cn != null) {
        componentNames.add(cn);
    }
}

"enabled_notification_listeners"は非公開のフィールドになってるので直で入れてるけど、リフレクションで取ってきてもいいかもね

ちなみに設定アプリのNotificationListenerServiceを有効/無効にする画面に行くためのIntentはこんな感じで作れます

public static Intent createStartNotificationSettingIntent() {
    Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
    return intent;
}

0 件のコメント:

コメントを投稿