Content providers and content watchers

Source: Internet
Author: User

1, create a content provider, inherit ContentProvider, as one of the four components, ContentProvider need to be configured in the configuration file

<provider             android:name="cn.itcast.mobilesafe.provider.AppLockedProvider"              android:authorities="cn.itcast.applockprovider">        </ Provider>

2.

 PackageCn.itcast.mobilesafe.provider;ImportCn.itcast.mobilesafe.db.dao.AppLockedDao;ImportAndroid.content.ContentProvider;Importandroid.content.ContentValues;ImportAndroid.content.UriMatcher;ImportAndroid.database.Cursor;ImportAndroid.net.Uri;ImportAndroid.util.Log;/*** This is the program lock content provider * When the data in the database changes, you need to update the data in a timely manner *@authorYGL **/ Public classApplockedproviderextendsContentProvider {Private Static Final intINSERT = 10; Private Static Final intDELETE = 11; Private Static FinalString TAG = "Applockedprovider"; //defines the match code for a URI (the return value when it does not match)    PrivateApplockeddao Appdao; Private StaticUrimatcher matcher=NewUrimatcher (Urimatcher.no_match); Private StaticUri urichange=uri.parse ("Content://cn.itcast.applockprovider"); Static{Matcher.adduri ("Cn.itcast.applockprovider", "Insert", INSERT); Matcher.adduri ("Cn.itcast.applockprovider", "delete", DELETE); }         Public BooleanonCreate () {Appdao=NewApplockeddao (GetContext ()); return false; }     PublicCursor query (Uri uri, string[] projection, string selection, string[] Selectionargs, string sortOrder) { //TODO auto-generated Method Stub        return NULL; }     PublicString getType (Uri uri) {//TODO auto-generated Method Stub        return NULL; } /** When data is inserted by the content provider, the data can be inserted into the database. * @see Android.content.contentprovider#insert (Android.net.Uri, Android.content.ContentValues)*/     Publicuri insert (URI uri, contentvalues values) {intresult=Matcher.match (URI); if(result==INSERT) {String Packname= (String) values.get ("Packname"); LOG.I (TAG,"The package to change is named" +packname); //Create a content watcher to tell the content provider that the data needs to be changedAppdao.add (packname); GetContext (). Getcontentresolver (). Notifychange (Urichange,NULL); }Else{            Throw NewIllegalArgumentException ("URI address is incorrect"); }        return NULL; }/** Delete data from the database when the content provider has a data delete operation * @see Android.content.contentprovider#delete (Android.net.Uri, java.lang.String, Java.lang.string[])*/     Public intDelete (Uri Uri, String selection, string[] selectionargs) {intresult=Matcher.match (URI); if(result==DELETE) {String Packname=selectionargs[0];            Appdao.delete (Packname); GetContext (). Getcontentresolver (). Notifychange (Urichange,NULL); }Else{            Throw NewIllegalArgumentException ("URI address is incorrect"); }        return0; }     Public intupdate (URI Uri, contentvalues values, String selection, string[] selectionargs) {//TODO auto-generated Method Stub        return0; }}

3. Operation of data via content provider (Lockappactivity.java)

 if(Appdao.find (PackageName)) {//Appdao.delete (PackageName); //working with content providers for data            getcontentresolver (). Delete (Uri.parse ("Content://cn.itcast.applockprovider/delete"), NULL, new string[]{packagename});         Iv_applock.setimagedrawable (Getresources (). getdrawable (r.drawable.unlocked)); }Else{         //Appdao.add (PackageName); //working with content providers for data           contentvalues cv= New Contentvalues ();          Cv.put ("Packname", PackageName); Getcontentresolver (). Insert (Uri.parse ("Content://cn.itcast.applockprovider/insert" ), CV);    Iv_applock.setimagedrawable (Getresources (). getdrawable (r.drawable.locked)); }

4, the Content observer observes the content provider, detects whether the data inside is changed (Applockservice.java)

true,new myobserve (new Handler ()));
/** Content Viewer*/    Private classMyobserveextendscontentobserver{ PublicMyobserve (Handler Handler) {Super(handler); }         Public voidOnChange (BooleanSelfchange) {            Super. OnChange (Selfchange); //re-update the contents of content providers when they are found to be changedpacknameslist=Appdao.getallpacknames (); }                }

Content providers and content watchers

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.