且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android 获取未读短信未接电话数目

更新时间:2022-08-13 10:07:18

引用:http://blog.csdn.net/zhuangxujun/article/details/7035430

[java] view plaincopy
  1. <pre name="code" class="java"></pre><pre name="code" class="java"><pre name="code" class="java">if (mIntentReceiver == null) {  
  2.   
  3.   
  4.             mIntentReceiver = new MissedCallIntentReceiver();  
  5.             final IntentFilter myFilter = new IntentFilter();  
  6.                     myFilter.addAction("com.android.phone.NotificationMgr.MissedCall_intent");  
  7.             getApplicationContext().registerReceiver(mIntentReceiver, myFilter);  
  8.   
  9.   
  10.                 }  
  11.                 getApplicationContext().getContentResolver().registerContentObserver(Uri.parse("content://mms-sms/"), true,   
  12.                                                                                             new newMmsContentObserver(getApplicationContext(),   myHandler)); </pre><br>  
  13. <pre></pre>  
  14. <pre name="code" class="java"private  class MissedCallIntentReceiver extends BroadcastReceiver {</pre><pre name="code" class="java">                @Override  
  15.         public void onReceive(Context context, Intent intent) {  
  16.                        missedCallCount=intent.getIntExtra(MISSECALL_EXTRA, 0);//未接电话数目  
  17.                        
  18.         }  
  19.     }  
  20.   
  21.   
  22.     private void findNewSmsCount(Context ctx){  
  23.         Cursor  csr = null;  
  24.         try {  
  25.             csr = getApplicationContext().getContentResolver().query(Uri.parse("content://sms"), null,"type = 1 and read = 0"nullnull);  
  26.         } catch (Exception e) {  
  27.              e.printStackTrace();  
  28.         } finally {  
  29.              csr.close();  
  30.         }  
  31.         newSmsCount=csr.getCount(); //未读短信数目  
  32.         Log.v(TAG,"newSmsCount="+newSmsCount);  
  33.     }  
  34.   
  35.     private void findNewMmsCount(Context ctx){  
  36.         Cursor  csr = null;  
  37.         try {  
  38.              csr = getApplicationContext().getContentResolver().query(Uri.parse("content://mms/inbox"), null,  
  39.                       "read = 0"nullnull);  
  40.         } catch (Exception e) {  
  41.              e.printStackTrace();  
  42.         } finally {  
  43.              csr.close();  
  44.         }  
  45.          newMmsCount=csr.getCount();//未读彩信数目  
  46.     }  
  47. //监控短信,彩信数目变化  
  48.     public class newMmsContentObserver extends ContentObserver{  
  49.         private Context ctx;         
  50.      
  51.         public newMmsContentObserver(Context context, Handler handler) {       
  52.             super(handler);       
  53.             ctx = context;       
  54.         }       
  55.   
  56.     @Override      
  57.     public void onChange(boolean selfChange){     
  58.         Log.v(TAG,"newMmsContentObserver onChange:");  
  59.         newMmsCount=0;      
  60.     newSmsCount=0;          
  61.         findNewMmsCount(ctx);  
  62.     findNewSmsCount(ctx);  
  63.          
  64.        }  
  65.      }</pre><br>  
  66. <br>  
  67. <pre></pre>  
  68. <br>  
  69. <span style="font-size:18px; color:#ff0000"><strong>用于获取未读短信未接电话数目</strong></span><br>  
  70.        
  71.   
  72. </pre>