This example shows how Android realizes the number of missed calls and unread messages, which is very common in Android program development and is a very useful feature that is now shared for your reference. Specifically as follows:
First, unread SMS
First register observer, when there are new SMS or MMS will call the OnChange method, we can in the OnChange method to get unread SMS and MMS, and then do some UI processing!
The specific function code is as follows:
Private Contentobserver Newmmscontentobserver = new Contentobserver (new Handler ()) {public
void OnChange (Boolean Selfchange) {
int mnewsmscount = Getnewsmscount () + Getnewmmscount ();
}
};
private void Registerobserver () {
unregisterobserver ();
Getcontentresolver (). Registercontentobserver (Uri.parse ("Content://sms"), True,
newmmscontentobserver);
Getcontentresolver (). Registercontentobserver (Mmssms.content_uri, True,
newmmscontentobserver);
Private synchronized void Unregisterobserver () {
try {
if (newmmscontentobserver!= null) {
Getcontentresolver (). Unregistercontentobserver (Newmmscontentobserver);
if (newmmscontentobserver!= null) {
getcontentresolver (). Unregistercontentobserver (Newmmscontentobserver);
}
catch (Exception e) {
log.e (TAG, "unregisterobserver fail");
}
Number of unread messages received:
private int Getnewsmscount () {
int result = 0;
Cursor CSR = Getcontentresolver (). Query (Uri.parse ("content://sms"), NULL,
"type = 1 and read = 0", null, NULL);
if (CSR!= null) {Result
= Csr.getcount ();
Csr.close ();
}
return result;
}
Get the number of unread mms:
private int Getnewmmscount () {
int result = 0;
Cursor CSR = Getcontentresolver (). Query (Uri.parse ("Content://mms/inbox"),
null, "read = 0", null, NULL);
if (CSR!= null) {Result
= Csr.getcount ();
Csr.close ();
}
return result;
}
Second, missed calls
Missed calls cannot be monitored with observer, but when a new missed call is received, the system sends a broadcast COM.ANDROID.PHONE.NOTIFICATIONMGR.MISSEDCALL_ Intent (the number of missed calls displayed on the lock screen is the notification to monitor this broadcast implementation)
The specific function code is as follows:
Final Intentfilter filter = new Intentfilter ();
Filter.addaction ("Com.android.phone.NotificationMgr.MissedCall_intent");
Final Application application = Getapplication ();
Application.registerreceiver (New Broadcastreceiver () {
@Override public
void OnReceive Intent Intent) {
String action = intent.getaction ();
if (Action!= null && "Com.android.phone.NotificationMgr.MissedCall_intent". Equals (Action)) {
int Mmisscallcount = Intent.getextras (). GetInt ("Missedcallnumber");}}
, filter);
Broadcasts are only sent when there is a new missed call, but if an old missed call is not read, the above broadcast will not get the data, it must be looked up from the database.
The functional code is as follows:
private int Readmisscall () {
int result = 0;
Cursor Cursor = Getcontentresolver (). Query (CallLog.Calls.CONTENT_URI, new string[] {
calls.type
}, "Type=? And new=? ", new string[] {
calls.missed_type +" "," 1 "
}," date desc ");
if (cursor!= null) {Result
= Cursor.getcount ();
Cursor.close ();
}
return result;
}
I believe the examples mentioned in this article can play a reference role for the development of Android program.