Android notification rights

Source: Internet
Author: User

Notification access rights open mode

Settings--Beep and notification--notification rights.

Specific interface

When there is a need to have notification rights app:

Does not exist when you need to have a notification rights app:

After the user has checked the check box for the app, the system bounces dialog if the user needs further confirmation:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>

mainly related to documents:

/packages/apps/Settings/src/COM/android/ Settings/notification/Notificationaccesssettings.java

?/Packages/apps/Settings/src/com/android/ Settings/notification/Managedservicesettings.java

Database involved:

Data/data/com.android.providers.settings

Specific explanation: /packages/apps/Settings/src/com/android/ Settings/notification/Notificationaccesssettings.java
public class Notificationaccesssettings extends Managedservicesettings {private static final String TAG = Notification    AccessSettings.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 = * Database field. Infer whether the app has notification rights by reading the fields in the database. There is the application of the corresponding checkbox in the interface is checked, * when the application of the corresponding checkbox is not checked, when the user check will be prompted after the popup box.         Once determined, this application information is written to the database through this field.        * */c.setting = Settings.Secure.ENABLED_NOTIFICATION_LISTENERS;        C.intentaction = Notificationlistenerservice.service_interface; /* * The application needs to declare this service permission in the manifest file to be detected before it is displayed to the same time use the interface * */c.permission = Android.        Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE;        C.noun = "Notification listener"; /* The title of the dialog that pops up after the user is ticked */C.warningdialogtitle = r.string.notification_listener_security_warning_tItle;        /* The contents of the dialog pop-up after the user tick */c.warningdialogsummary = r.string.notification_listener_security_warning_summary;        /* In the current system does not exist regardless of what needs to use the notification rights of the application, the notification access interface will be prompted */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 = FA    Lse    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 ();    }    }; /* You need to update the application list when you hear changes in the number of applications */private final broadcastreceiver mpackagereceiver = new Broadcastreceiver () {@Overrid        e public void OnReceive (context context, Intent Intent) {updatelist ();    }    }; /* The user will be ticked and the corresponding dialog wait for the user to further confirm that you want to open the notification rights for this app */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 (Savedinstanc            EState);            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 VO ID OnClick (dialoginterface dialog, int id) {Menabledservices.add (CN);//Add application information to hashset& Lt                            Componentname> in Saveenabledservices ();//Database write operation} }). 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 savedinstancest        ATE) {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);//Application Join Filter.addaction (intent.action_package_changed);//Application change        Filter.addaction (intent.action_package_removed);//Application uninstall Filter.addaction (intent.action_package_replaced);//Application update        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); */* Load the app with notification rights from the database and deposit its information in hashset<componentname>. */private void loadenabledservices () {menabledservices.clear ();//Empty hashset<componentname> first. Make sure the data is up to date from the database read final String flat = Settings.Secure.getString (MCR, mconfig.setting);//Database read if (flat! = null & amp;&! "".            Equals (flat)) {final string[] names = Flat.split (":"); for (int i = 0; i < names.length; i++) {final componentname cn = componentname.unflattenfromstring (Name                S[i]);                if (CN = null) {Menabledservices.add (CN);   }            }        } }/* Data inventory operation */private void saveenabledservices () {StringBuilder SB = null;            for (ComponentName cn:menabledservices) {if (SB = = null) {SB = new StringBuilder ();            } else {sb.append (': ');        } sb.append (Cn.flattentostring ()); }/* Data inventory operation */Settings.Secure.putString (MCR, mconfig.setting, SB! = null? sb.t    Ostring (): "");        }/* Update app Display list */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.se        Cure.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 Ser        vices = 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 = Installedservi            Ces.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.packagena        Me, info.name);    Return Menabledservices.contains (CN); } @Override public void Onlistitemclick (ListView l, View v, int position, long ID) {ServiceInfo info = mlist        Adapter.getitem (position);        Final ComponentName cn = new ComponentName (Info.packagename, info.name); if (Menabledservices.contains (CN)) {//un-tick//The simple version:disabling menabledservices            . Remove (CN);        Saveenabledservices (); } else {//select tick to fill out dialog//show a scary dialog new Scarywarningdialogfragment (). Setserviceinfo (CN, Info.loadlabel (MPM). toString ()). Show (Getfragmentmanager (), "Dial        OG ");        }} private static class Viewholder {ImageView icon;        TextView name;        CheckBox checkbox;    TextView description; }/* For app list loading display */Private class Servicelistadapter extends Arrayadapter<serviceinfo> {final Layouti        Nflater 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, Pare            NT, FALSE);            Viewholder h = new Viewholder ();            H.icon = (ImageView) V.findviewbyid (R.id.icon);//Application Icon H.name = (TextView) V.findviewbyid (r.id.name);//Application name H.checkbox = (checkbox) V.findviewbyid (R.id.checkbox);//tick box H.description = (TextView) V.findviewbyid (r.i            d.description);//Application Descriptive narrative 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>

Database related Information

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>

The database field applies the appropriate Information format: Package name/service: Package name/service. The information between the two applications is separated by ":".

Android notification rights

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.