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