This example describes the listener usage of Android programming. Share to everyone for your reference, specific as follows:
This is done by listening to the content provider data changes and listening to send SMS to explain the listener. If the user provider data changes, the listener can immediately receive a record of the operation of the database, and the listener is the mechanism to use notifications, if not the mechanism of notification can also do, it is constantly querying the database, so the efficiency will be very low. With the notification mechanism, when the user sends a record to the database, Contentobserver can get it immediately, and we can process the data.
Monitor changes in content provider data
1. Monitoring when the content provider can notify other programs that the data has changed
Get Contentresolver from the Getcontentresolver () method of the context
Call its Notifychange () method to send a data modification notification
2. Can monitor data changes through Contentobserver in other programs
Get Contentresolver from the Getcontentresolver () method of the context
Call its Registercontentobserver () method to specify that a URI be registered Contentobserver
Custom Contentobserver, overriding the OnChange () method to get the data
For example, when the user inserts the data to monitor the notification:
Public URI insert (URI uri, contentvalues values) {
Sqlitedatabase db = Helper.getwritabledatabase ();
Switch (Matcher.match (URI)) {case
Person_all:
Long id = db.insert (' person ', ' id ', values);
Listener Notification
GetContext (). Getcontentresolver (). Notifychange (URI, null);
Return Contenturis.withappendedid (URI, id);
Default:
throw new IllegalArgumentException ("Unmatch uri:" + uri);
}
Listening in another project, once the user inserts, I can automatically get to the user to insert the record immediately:
public class Mainactivity extends activity {public void onCreate (Bundle savedinstancestate) {super.oncreate (saved
Instancestate);
Setcontentview (R.layout.main); Getcontentresolver (). Registercontentobserver (Uri.parse ("Content://cn.itcast.provider.itcast/person"), True, new
Myobserver (New Handler ());
Class Myobserver extends Contentobserver {public myobserver (Handler Handler) {super (Handler); ///When the monitor hears the data change, call this method and query the newly added data out public void OnChange (Boolean selfchange) {contentresolver resolver = ge
Tcontentresolver ();
Uri uri = uri.parse ("Content://cn.itcast.provider.itcast/person");
Cursor Cursor = Resolver.query (URI, new string[] {"id", "name", "Phone", "balance"}, NULL, NULL, "ID DESC LIMIT 1");
while (Cursor.movetonext ()) {System.out.print (cursor.getstring (0) + "");
System.out.print (cursor.getstring (1) + "");
System.out.print (cursor.getstring (2) + ""); System.out.println (cursoR.getstring (3));
}
}
}
}
Listening to send text messages
The 1.Android system provides a provider to query the SMS and sends a change notification when the message is sent.
2. Define a observer monitor "content://sms"
3. In the onchange () method to query the user sent SMS "Content://sms/outbox"
4. Message sending information stored in database Date/date/com.android.providers.telephony
5. Permissions Required <uses-permission android:name= "Android.permission.READ_SMS"/>
Example:
public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Getcontentresolver (). Registercontentobserver (Uri.parse ("Content://sms"), True, new Smsobserver (New Handler ()); }//Listen to send SMS private final class Smsobserver extends Contentobserver {public smsobserver (Handler Handler) {super (ha
Ndler);
} public void OnChange (Boolean selfchange) {contentresolver resolver = Getcontentresolver ();
Find out the SMS URI uri = uri.parse ("Content://sms/outbox");
Cursor Cursor = Resolver.query (URI, new string[] {' Date ', ' address ', ' body '}, NULL, NULL, "_id desc limit 1");
if (Cursor.movetonext ()) {Long ms = Cursor.getlong (0);
String date = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (new date (ms));
String address = cursor.getstring (1);
String BODY = cursor.getstring (2);
SYSTEM.OUT.PRINTLN (date + "" + Address + "" + body); }
}
}
I hope this article will help you with your Android programming.