Android implements a method to obtain the number of missed calls and unread messages

  • 2020-06-07 05:16:49
  • OfStack

This article demonstrates the Android method to obtain the number of missed calls and unread messages, which is very common in the development of Android program and is a very practical function. Now it is Shared for your reference. The details are as follows:

1. Not reading text messages

First register Observer, when there is a new SMS or MMS will call onChange method, we can get unread SMS and MMS in onChange method, and then do some processing on UI!

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"); 
  } 
} 

Get the number of unread messages:


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; 
} 

2. Missed call

Not call cannot use Observer surveillance, but when there are new not call, the system will send a broadcast com. android. phone. NotificationMgr. MissedCall_intent (shown on the lock screen not call number is listening to the radio notice)

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(Context context, Intent intent) { 
    String action = intent.getAction(); 
    if (action != null && "com.android.phone.NotificationMgr.MissedCall_intent".equals(action)) { 
      int mMissCallCount = intent.getExtras().getInt("MissedCallNumber"); 
    } 
  } 
}, filter); 

The broadcast is only sent when there is a new missed call, but if there is an old missed call that has not been read, the broadcast above will not get the data and will have to be looked up in the database.

The function 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 that the examples described in this paper can play a reference role in the development of Android program.


Related articles: