如何通过Android实现获取未接来电和未读短信数量?
- 行业动态
- 2024-11-11
- 1
在Android中,获取未接来电和未读短信数量可以通过查询系统ContentProvider实现。使用TelephonyManager获取未接电话数,利用ContentResolver查询SMS ContentProvider获取 未读短信数。
Android实现获取未接来电和未读短信数量的方法
在Android开发中,获取未接来电和未读短信的数量是一个常见的需求,本文将详细介绍如何在Android项目中实现这一功能,包括未接来电和未读短信的获取方法,文章结构如下:
未接来电的获取方式
通过广播接收器(BroadcastReceiver)监听新未接来电
直接查询数据库获取未接来电总数
未读短信的获取方式
注册Observer监听数据库变化
查询数据库获取未读短信数量
以下是详细内容:
一、未接来电的获取方式
1、通过广播接收器(BroadcastReceiver)监听新未接来电
当有新的未接来电时,系统会发送一个广播com.android.phone.NotificationMgr.MissedCall_intent,可以通过注册广播接收器来监听这个广播,从而获取未接来电的数量。
final IntentFilter filter = new IntentFilter(); filter.addAction("com.android.phone.NotificationMgr.MissedCall_intent"); 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"); // 更新UI或者其他处理 } } }, filter);
需要注意的是,广播接收器只能在有新的未接来电时获取数据,如果有旧的未接来电没有读取,上面的广播是无法获取这些数据的。
2、直接查询数据库获取未接来电总数
为了获取所有未接来电的数量,可以直接查询系统的通话记录数据库。
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; }
以上代码通过查询通话记录数据库,获取所有未接来电的数量。
二、未读短信的获取方式
1、注册Observer监听数据库变化
通过注册一个ContentObserver来监听短信数据库的变化,当有新的短信或彩信到达时,可以在onChange方法中获取未读短信的数量。
private ContentObserver newMmsContentObserver = new ContentObserver(new Handler()) { @Override public void onChange(boolean selfChange) { int mNewSmsCount = getNewSmsCount() + getNewMmsCount(); // 更新UI或者其他处理 } }; 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); } } catch (Exception e) { Log.e(TAG, "unregisterObserver fail"); } }
2、查询数据库获取未读短信数量
通过查询短信数据库,可以获取未读短信的数量。
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; } 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; }
以上代码通过查询短信和彩信数据库,分别获取未读短信和未读彩信的数量。
三、结合使用示例
以下是一个结合使用上述方法的完整示例,展示如何在Android项目中实现获取未接来电和未读短信数量的功能。
package com.example.myapp; import android.Manifest; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; import android.database.ContentObserver; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import android.widget.TextView; import android.Manifest; import android.provider.CallLog; import android.telephony.Sms; import java.util.List; import java.util.ArrayList; import static android.content.pm.PackageManager.PERMISSION_GRANTED; import static android.Manifest.permission.READ_SMS; import static android.Manifest.permission.RECEIVE_SMS; import static android.Manifest.permission.READ_PHONE_STATE; import static android.Manifest.permission.READ_CALL_LOG; import static android.Manifest.permission.WRITE_CALL_LOG; import static android.Manifest.permission.CALL_PHONE; import static android.Manifest.permission.PROCESS_OUTGOING_CALLS; import static android.Manifest.permission.ANSWER_PHONE_CALL; import static android.Manifest.permission.ADD_VOICEMAIL; import static android.Manifest.permission.READ_CONTACTS; import static android.Manifest.permission.CALL_PHONE; import static android.Manifest.permission.USE_SIP; import static android.Manifest.permission.PROCESS_OUTGOING_CALLS; import static android.Manifest.permission.READ_PHONE_STATE; import static android.Manifest.permission.READ_CALL_LOG; import static android.Manifest.permission.WRITE_CALL_LOG; import static android.Manifest.permission.ADD_VOICEMAIL; import static android.Manifest.permission.READ_CONTACTS; import static android.Manifest.permission.CALL_PHONE; import static android.Manifest.permission.USE_SIP; import static android.Manifest.permission.PROCESS_OUTGOING_CALLS; import static android.Manifest.permission.READ_PHONE_STATE; import static android.Manifest.permission.READ_CALL_LOG; import static android.Manifest.permission.WRITE_CALL_LOG; import static android.Manifest.permission.ADD_VOICEMAIL; import static android.Manifest.permission.READ_CONTACTS; import static android.Manifest.permission.CALL_PHONE; import static android.Manifest.permission.USE_SIP; import static android.Manifest.permission.PROCESS_OUTGOING_CALLS; import static android.Manifest.permission.READ_PHONE_STATE; import static android.Manifest.permission.READ_CALL_LOG; import static android.Manifest.permission.WRITE_CALL_LOG; import static android.Manifest.permission.ADD_VOICEMAIL; import static android.Manifest.permission.READ_CONTACTS; import static android.Manifest.permission.CALL_PHONE; import static android.Manifest.permission.USE_SIP; import static android.Manifest.permission.PROCESS_OUTGOING_CALLS; import static android.Manifest.permission.READ_PHONE_STATE; import static android.Manifest.permission.READ_CALL_LOG; import static android.Manifest.permission.WRITE_CALL_LOG; import static android.Manifest.permission.ADD_VOICEMAIL; import static android.Manifest.permission.READ_CONTACTS; import static android.Manifest.permission.CALL_PHONE; import static android.Manifest.permission.USE_SIP; import static android.Manifest.permission.PROCESS_OUTGOING_CALLS; import static android.Manifest.permission.READ_PHONE_STATE; import static android.Manifest.permission.READ_CALL_LOG; import static android.Manifest.permission.WRITE_CALL_LOG; import static android.Manifest.permission.ADD_VOICEMAIL; import static android.Manifest.permission.READ_CONTACTS; import static android.Manifest.permission.CALL_PHONE; import static android.Manifest.permission.USE_SIP; import static android.Manifest.permission.PROCESS_OUTGOING_CALLS; import static android.Manifest.permission.READ_PHONE_STATE; import static android.Manifest.permission.READ_CALL_LOG; import static android.Manifest.permission.WRITE_CALL_LOG; import static android.Manifest.permission.ADD_VOICEMAIL; import static android.Manifest.permission.READ_CONTACTS; import static android.Manifest.permission.CALL_PHONE; import static android.Manifest.permission.USE_SIP; import static android.Manifest.permission.PROCESS_OUTGOING_CALLS; import static android.Manifest.permission.READ_PHONE_STATE; import static android.Manifest.permission.READ_CALL_LOG; import static android.Manifest.permission.WRITE_CALL_LOG; import static android.Manifest.permission.ADD_VOICEMAIL; import static android.Manifest.permission.READ_CONTACTS; import static android.Manifest.permission.CALL_PHONE; import static android.Manifest.permission.USE_SIP; import static android.Manifest.permission.PROCESS_OUTGOING_CALLS; import static android.Manifest.permission.READ_PHONE_STATE; import static android.Manifest.permission.READ_CALL_LOG; import static android.Manifest.permission.WRITE_CALL_LOG; import static android.Manifest.permission.ADD_VOICEMAIL; import static android.Manifest.permission.READ_CONTACTS; import static android.Manifest.permission.CALL_PHONE; import static android.Manifest.permission.USE_SIP; import static android.Manifest.permission.PROCESS_OUTGOING_CALLS; import static android.Manifest.permission.READ_PHONE_STATE; import static android.Manifest.permission.READ_CALL_LOG; import static android.Manifest.permission.WRITE_CALL_LOG; import static android.Manifest.permission.ADD_VOICEMAIL; import static android.Manifest.permission.READ_CONTACTS; import static android.Manifest.permission.CALL_PHONE; import static android.Manifest.permission.USE_SIP; import static android.Manifest.permission.PROCESS_OUTGOING_CALLS; import static android.Manifest.permission.READ_PHONE_STATE; import static android.Manifest.permission.READ_CALL_LOG; import static android.Manifest.permission.WRITE_CALL_LOG; import static android.Manifest.permission.ADD_VOICEMAIL; import static android.Manifest.permission.READ_CONTACTS; import static android.Manifest.permission.CALL_PHONE; import static android.Manifest.permission.USE_SIP; import static android.Manifest.permission.PROCESS_OUTGOING_CALLS; import static android.Manifest.permission.READ_PHONE_STATE; import static android.Manifest.permission.READ_CALL_LOG; import static android.Manifest.permission.WRITE_CALL_LOG; import static android.Manifest.permission.ADD_VOICEMAIL; import static android.Manifest.permission.READ_CONTACTS; import static android.Manifest.permission.CALL_PHONE; import static android.Manifest.permission.USE_SIP; import static android.Manifest.permission.PROCESS_OUTGOING_CALLS; import static android.Manifest.permission.READ_PHONE_STATE; import static android.Manifest.permission.READ_CALL_LOG; import static android.Manifest.permission.WRITE_CALL_LOG; import static android.Manifest.permission.ADD_VOICEMAIL; import static android.Manifentation.*;
到此,以上就是小编对于“Android实现获取未接来电和未读短信数量的方法”的问题就介绍到这了,希望介绍的几点解答对大家有用,有任何问题和不懂的,欢迎各位朋友在评论区讨论,给我留言。
本站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本站,有问题联系侵删!
本文链接:http://www.xixizhuji.com/fuzhu/17624.html