通知使用权打开方式

设置——提示音和通知——通知使用权。

具体界面如图:

存在须要拥有通知使用权应用时:

不存在须要拥有通知使用权应用时:

用户为应用勾选复选框后系统弹dialog须要用户进一步确认时:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

主要涉及文件:

/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

具体解说:

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为勾选状态,
*应用相应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";
/*用户勾选后弹出的dialog中的标题*/
c.warningDialogTitle = R.string.notification_listener_security_warning_title;
/*用户勾选后弹出的dialog中的内容*/
c.warningDialogSummary = R.string.notification_listener_security_warning_summary;
/*当前系统中不存在不论什么须要使用通知使用权的应用时,通知使用权界面会有相应提示*/
c.emptyText = R.string.no_notification_listeners;
return c;
} @Override
protected Config getConfig() {
return CONFIG;
} public static int getListenersCount(PackageManager pm) {
return getServicesCount(CONFIG, pm);
} public static int getEnabledListenersCount(Context context) {
return getEnabledServicesCount(CONFIG, context);
}
}

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

public abstract class ManagedServiceSettings extends ListFragment {
private static final boolean SHOW_PACKAGE_NAME = false; private final Config mConfig;
private PackageManager mPM;
private ContentResolver mCR; private final HashSet<ComponentName> mEnabledServices = new HashSet<ComponentName>();
private ServiceListAdapter mListAdapter; abstract protected Config getConfig(); public ManagedServiceSettings() {
mConfig = 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();
}
};
/*用户勾选后弹出相应dialog等待用户进一步确认要为此应用打开通知使用权*/
public class ScaryWarningDialogFragment extends DialogFragment {
static final String KEY_COMPONENT = "c";
static final String KEY_LABEL = "l"; public ScaryWarningDialogFragment setServiceInfo(ComponentName cn, String label) {
Bundle args = new Bundle();
args.putString(KEY_COMPONENT, cn.flattenToString());
args.putString(KEY_LABEL, label);
setArguments(args);
return this;
} @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Bundle args = getArguments();
final String label = args.getString(KEY_LABEL);
final ComponentName cn = ComponentName.unflattenFromString(args.getString(KEY_COMPONENT)); final String title = getResources().getString(mConfig.warningDialogTitle, label);
final String summary = getResources().getString(mConfig.warningDialogSummary, label);
return new AlertDialog.Builder(getActivity())
.setMessage(summary)
.setTitle(title)
.setCancelable(true)
.setPositiveButton(android.R.string.ok,//
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mEnabledServices.add(cn);//加入应用信息到HashSet<ComponentName>中
saveEnabledServices();//数据库写操作
}
})
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// pass
}
})
.create();
}
} @Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle); mPM = getActivity().getPackageManager();
mCR = getActivity().getContentResolver();
mListAdapter = new ServiceListAdapter(getActivity());
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.managed_service_settings, container, false);
TextView empty = (TextView) v.findViewById(android.R.id.empty);
empty.setText(mConfig.emptyText);
return v;
} @Override
public void onResume() {
super.onResume();
updateList(); // listen for package changes
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_ADDED);//应用加入
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);//应用改变
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);//应用卸载
filter.addAction(Intent.ACTION_PACKAGE_REPLACED);//应用更新
filter.addDataScheme("package");
getActivity().registerReceiver(mPackageReceiver, filter); mCR.registerContentObserver(Settings.Secure.getUriFor(mConfig.setting),
false, mSettingsObserver);
} @Override
public void onPause() {
super.onPause(); getActivity().unregisterReceiver(mPackageReceiver);
mCR.unregisterContentObserver(mSettingsObserver);
} /*从数据库中载入拥有通知使用权的应用,并将其信息存入到HashSet<ComponentName>中。*/
private void loadEnabledServices() {
mEnabledServices.clear();//首先清空HashSet<ComponentName>。确保数据最新从数据库读取
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);
} protected static int getEnabledServicesCount(Config config, Context context) {
final String flat = Settings.Secure.getString(context.getContentResolver(), config.setting);
if (flat == null || "".equals(flat)) return 0;
final String[] components = flat.split(":");
return components.length;
} protected static int getServicesCount(Config c, PackageManager pm) {
return getServices(c, null, pm);
} private static int getServices(Config c, ArrayAdapter<ServiceInfo> adapter, PackageManager pm) {
int services = 0;
if (adapter != null) {
adapter.clear();
}
final int user = ActivityManager.getCurrentUser(); List<ResolveInfo> installedServices = pm.queryIntentServicesAsUser(
new Intent(c.intentAction),
PackageManager.GET_SERVICES | PackageManager.GET_META_DATA,
user); for (int i = 0, count = installedServices.size(); i < count; i++) {
ResolveInfo resolveInfo = installedServices.get(i);
ServiceInfo info = resolveInfo.serviceInfo; if (!c.permission.equals(info.permission)) {
Slog.w(c.tag, "Skipping " + c.noun + " service "
+ info.packageName + "/" + info.name
+ ": it does not require the permission "
+ c.permission);
continue;
}
if (adapter != null) {
adapter.add(info);
}
services++;
}
return services;
} private boolean isServiceEnabled(ServiceInfo info) {
final ComponentName cn = new ComponentName(info.packageName, info.name);
return mEnabledServices.contains(cn);
} @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)) {
//取消勾选
// the simple version: disabling
mEnabledServices.remove(cn);
saveEnabledServices();
} else {
//选择勾选后填出dialog
// show a scary dialog
new ScaryWarningDialogFragment()
.setServiceInfo(cn, info.loadLabel(mPM).toString())
.show(getFragmentManager(), "dialog");
}
} private static class ViewHolder {
ImageView icon;
TextView name;
CheckBox checkbox;
TextView description;
} /*用于应用列表载入显示*/
private class ServiceListAdapter extends ArrayAdapter<ServiceInfo> {
final LayoutInflater mInflater; ServiceListAdapter(Context context) {
super(context, 0, 0);
mInflater = (LayoutInflater)
getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
} public boolean hasStableIds() {
return true;
} public long getItemId(int position) {
return position;
} public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
v = newView(parent);
} else {
v = convertView;
}
bindView(v, position);
return v;
} public View newView(ViewGroup parent) {
View v = mInflater.inflate(R.layout.managed_service_item, parent, false);
ViewHolder h = new ViewHolder();
h.icon = (ImageView) v.findViewById(R.id.icon);//应用图标
h.name = (TextView) v.findViewById(R.id.name);//应用名
h.checkbox = (CheckBox) v.findViewById(R.id.checkbox);//勾选框
h.description = (TextView) v.findViewById(R.id.description);//应用描写叙述
v.setTag(h);
return v;
} public void bindView(View view, int position) {
ViewHolder vh = (ViewHolder) view.getTag();
ServiceInfo info = getItem(position); vh.icon.setImageDrawable(info.loadIcon(mPM));
vh.name.setText(info.loadLabel(mPM));
if (SHOW_PACKAGE_NAME) {
vh.description.setText(info.packageName);
vh.description.setVisibility(View.VISIBLE);
} else {
vh.description.setVisibility(View.GONE);
}
vh.checkbox.setChecked(isServiceEnabled(info));
}
} protected static class Config {
String tag;
String setting;
String intentAction;
String permission;
String noun;
int warningDialogTitle;
int warningDialogSummary;
int emptyText;
}
<p>}
</p>

数据库相关信息

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

数据库字段相应应用信息格式:包名/service:包名/service。两应用间信息用”:“隔开。

Android之通知使用权的更多相关文章

  1. android: 使用通知

    8.1   使用通知 通知(Notification)是 Android 系统中比较有特色的一个功能,当某个应用程序希望向 用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现.发 ...

  2. Android消息通知(notification)和PendingIntent传值

    通知栏的自定义布局:转:http://blog.csdn.net/vipzjyno1/article/details/25248021 拓展 实现自定义的通知栏效果: 这里要用到RemoteViews ...

  3. Android Notification通知详细解释

    Android Notification通知具体解释  Notification: (一).简单介绍:         显示在手机状态栏的通知. Notification所代表的是一种具有全局效果的通 ...

  4. Android灯光系统--通知灯深入分析【转】

    本文转自:https://www.cnblogs.com/lkq1220/p/6406261.html Android灯光系统--通知灯深入分析 通知的类别 声音 振动 闪灯 APP如何发出通知灯请求 ...

  5. 【转】Android中通知的提示音、震动和LED灯效果小例子

    通知(Notification)是 Android 系统中比较有特色的一个功能,当某个应用程序希望向用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现.发出一条通知后,手机最上方 ...

  6. Android灯光系统--通知灯深入分析

    Android灯光系统--通知灯深入分析 通知的类别 声音 振动 闪灯 APP如何发出通知灯请求 getSystemService(获得通知服务) 构造notification 类别 其他参数(颜色, ...

  7. Android Service 通知Activity更新界面的方法研究

    Android Service 通知Activity更新界面的方法研究   Android的最重要的组件式service和activity,那么在使用的过程中,我们最常遇到的问题是他们之间的通信问题. ...

  8. Android Notification通知简介

    Android Notification通知简介 根据activity的生命周期,在activity不显示时,会执行onStop函数(比如按下home键),所以你在onStop函数(按退出键除外)里面 ...

  9. Android Notification通知详解

    根据activity的生命周期,在activity不显示时,会执行onStop函数(比如按下home键),所以你在onStop函数(按退出键除外)里面把notification放在通知栏里,再此显示时 ...

随机推荐

  1. Word Search——经典题(还没细看)

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  2. 小米路由器3-R3 刷固件

    1.刷机前的路由器升级准备 1-1.首先进入路由器原声后台:miwifi.com 1-2.在右上角,点击系统升级.在系统版本下边选择手动升级,选择资源包里的:“miwifi_r3_all_55ac7_ ...

  3. 关于 Unity WebGL 的探索(一)

    到今天为止,项目已经上线一个多月了,目前稳定运行,各种 bug 也是有的.至少得到了苹果的两次推荐和 TapTap 一次首页推荐,也算是结项后第一时间对我们项目的一个肯定. 出于各种各样的可描述和不可 ...

  4. Codeforces GYM 101968 A. Tree Game

    差点自闭,感谢大佬帮忙找bug 题目:https://codeforces.com/gym/101968/problem/A 找树的重心+思维 找到树的重心,如果重心只有一个,以重心为根节点dfs,求 ...

  5. 【大杀器】利用划分树秒杀区间内第k大的数

    最近看了一道题,大概就是给出一个序列,不断询问其子区间内第k大的数,下面是个截图 绕了一圈没找到中文版题目,if(你是大佬) then 去看截图:else{我来解释:给出一个整数n,和一个整数m,分别 ...

  6. 【Nginx】初试反向代理:反向代理的原理和用途

    Nginx是一个轻量级的服务器,是一个俄罗斯的开发者开发的开源软件.Nginx具有占内存小.并发能力高的特点,底层采用epoll(Linux2.6+)和kqueue(FREEBSD)网络I/O模型,相 ...

  7. 树形dp入门(poj 2342 Anniversary party)

    题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知每个人的活跃指数和上司关系(当然不可能存在环),求邀请哪些人(多少人)来能使得晚 ...

  8. java.lang.NegativeArraySizeException

    两台android设备发送图片. 发送端: Socket socket = null; try { socket = new Socket(ip, 8888); byte[] bytes = Scre ...

  9. [BZOJ2654]tree(二分+Kruskal)

    2654: tree Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 2733  Solved: 1124[Submit][Status][Discus ...

  10. 【贪心】【线性基】bzoj2460 [BeiJing2011]元素

    题意:让你求一些数在XOR下的带权极大无关组. 带权极大无关组可以用贪心,将这些数按权值从大到小排序之后,依次检验其与之前的数是否全都线性无关.可以用线性基来搞. 可以用拟阵严格证明,不过也可以脑补一 ...