如何获取未接来电和未接来电短信数量
我想获取应用程序中未接来电和未读消息的计数。我想在用户单击计数时打开相关应用程序。
现在最大的问题是如何得到计数?
我在网上搜索但找不到任何解决方案。
提前致谢。
I want to get the count of missed calls and unread messages in my application. and I'd like to open the relevant application when user click on the count.
Now biggest problem is how to get the count?
I searched online but couldn't find any solution.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
http://developer.android.com/reference/android/provider/ CallLog.Calls.html
看一下这个
CallLog
类。您所需要做的就是查询电话中是否有任何呼叫,然后提取未接的电话(或在查询电话时在选择参数中执行此操作)。这同样适用于消息。 SMS 存储在内容提供程序中的“content://sms/”下,然后只需获取查询返回的游标中的行数即可。 :)
我希望这有帮助。
对于未接来电:
在 where 子句中设置数据选择的条件。在我们的例子中,我们需要类型等于 CallLog.Calls.MISSED_TYPE 的所有内容。我们选择项目呼叫者的姓名及其号码,当然您可以指定要查询的更多信息,例如号码类型,例如手机、家庭、工作。
该表达式相当于 SQL 查询,类似于:
SELECT CACHED_NAME, CACHED_NUMBER_LABEL, TYPE FROM CONTENT_URI WHERE TYPE=MISSED_TYPE
这需要将权限添加到 Manifest
以用于查询 SMS
ContentProvider
:您可以更深入地了解内容树,例如指定短信类型,例如:
content://sms/sent
或content://sms/inbox
并为query()
方法的第二个参数添加投影和选择,例如消息的名称、人员、状态(例如呼叫例子)。这需要许可:
http://developer.android.com/reference/android/provider/CallLog.Calls.html
Take a look at this
CallLog
class. All you need is to query the phone for any calls then extract missed one (оr do this when you are querying the phone, in the selection arguments). The same applies for the messages. SMS are stored in the Content provider under"content://sms/"
Then just get the count of rows in the Cursor that is return by the query. :)
I hope this helps.
For missed calls:
In the where clause you set condition for selection of data. In our case we need everything which type equals
CallLog.Calls.MISSED_TYPE
. We select project the Name of the caller and his number, ofcourse you can specify more information to be queried like type of number like mobile, home, work.The expression is equivalent to SQL query, something like:
SELECT CACHED_NAME, CACHED_NUMBER_LABEL, TYPE FROM CONTENT_URI WHERE TYPE=MISSED_TYPE
This requires permissions to be added to the Manifest
For querying SMS
ContentProvider
:You can go deeper in the content tree like specifying the type of sms, like:
content://sms/sent
orcontent://sms/inbox
and add projection and selection for the second argument of thequery()
method like, name, person, status of the message (like the Calls example).This requires permission:
因为我没有足够的声誉来回答@Prasad 问题评论关于
错误 -> new Runnable(){} 类型的 getContentResolver() 未定义
getContentResolver()
是应用程序上下文的一部分,因此,如果您使用 BroadcastReceiver,请在onReceive() 中使用上下文
像这样的函数如果您在 Activity 中使用上面的代码,那么您可以使用
并确保使用 [Ctrl + Shift + O(O 不为零)] 来组织导入
Eclipse 导入的关键快捷键
As I don't have enough reputation to answer @Prasad question comment about
ERROR -> getContentResolver() is undefined for the type new Runnable(){}
getContentResolver()
is part of application context, so if you are using a BroadcastReceiver use context inonReceive()
function like thisIf you are using the code above inside an Activity, then you can use
also make sure to use [Ctrl + Shift + O (O not zero)] to organize imports
Key Shortcut for Eclipse Imports