Android realizes the method of obtaining the number of missed calls and unread SMS _android

Source: Internet
Author: User

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.

Related Article

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.