如何获取未接来电和未接来电短信数量

发布于 2024-12-07 13:39:23 字数 112 浏览 1 评论 0原文

我想获取应用程序中未接来电和未读消息的计数。我想在用户单击计数时打开相关应用程序。

现在最大的问题是如何得到计数?

我在网上搜索但找不到任何解决方案。

提前致谢。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空名 2024-12-14 13:39:23

http://developer.android.com/reference/android/provider/ CallLog.Calls.html

看一下这个 CallLog 类。您所需要做的就是查询电话中是否有任何呼叫,然后提取未接的电话(或在查询电话时在选择参数中执行此操作)。这同样适用于消息。 SMS 存储在内容提供程序中的“content://sms/”下

,然后只需获取查询返回的游标中的行数即可。 :)

我希望这有帮助。

对于未接来电:

String[] projection = {
    CallLog.Calls.CACHED_NAME,
    CallLog.Calls.CACHED_NUMBER_LABEL,
    CallLog.Calls.TYPE
};
String where = CallLog.Calls.TYPE + "=" + CallLog.Calls.MISSED_TYPE;          
Cursor c = this.getContentResolver().query(
    CallLog.Calls.CONTENT_URI,
    selection,
    where,
    null,
    null
);
c.moveToFirst();    
Log.d("CALL", ""+c.getCount()); //do some other operation
if (c.getCount() == SOME_VALUE_TO_START_APP_ONE) //...etc etc

在 where 子句中设置数据选择的条件。在我们的例子中,我们需要类型等于 CallLog.Calls.MISSED_TYPE 的所有内容。我们选择项目呼叫者的姓名及其号码,当然您可以指定要查询的更多信息,例如号码类型,例如手机、家庭、工作。
该表达式相当于 SQL 查询,类似于:SELECT CACHED_NAME, CACHED_NUMBER_LABEL, TYPE FROM CONTENT_URI WHERE TYPE=MISSED_TYPE

这需要将权限添加到 Manifest

<uses-permission android:name="android.permission.READ_LOGS"></uses-permission>

<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

以用于查询 SMS ContentProvider

Uri sms_content = Uri.parse("content://sms");
Cursor c = this.getContentResolver().query(sms_content, null,null, null, null);
c.moveToFirst();
Log.d("SMS COUNT", "" + c.getCount()); //do some other operation
// Here proceed with the what you wanted
if (c.getCount() == SOME_VALUE_TO_START_APP_ONE)//...etc etc

您可以更深入地了解内容树,例如指定短信类型,例如:content://sms/sentcontent://sms/inbox 并为 query() 方法的第二个参数添加投影和选择,例如消息的名称、人员、状态(例如呼叫例子)。

这需要许可:

<uses-permission android:name="android.permission.READ_SMS"></uses-permission>

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:

String[] projection = {
    CallLog.Calls.CACHED_NAME,
    CallLog.Calls.CACHED_NUMBER_LABEL,
    CallLog.Calls.TYPE
};
String where = CallLog.Calls.TYPE + "=" + CallLog.Calls.MISSED_TYPE;          
Cursor c = this.getContentResolver().query(
    CallLog.Calls.CONTENT_URI,
    selection,
    where,
    null,
    null
);
c.moveToFirst();    
Log.d("CALL", ""+c.getCount()); //do some other operation
if (c.getCount() == SOME_VALUE_TO_START_APP_ONE) //...etc etc

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

<uses-permission android:name="android.permission.READ_LOGS"></uses-permission>

<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

For querying SMS ContentProvider:

Uri sms_content = Uri.parse("content://sms");
Cursor c = this.getContentResolver().query(sms_content, null,null, null, null);
c.moveToFirst();
Log.d("SMS COUNT", "" + c.getCount()); //do some other operation
// Here proceed with the what you wanted
if (c.getCount() == SOME_VALUE_TO_START_APP_ONE)//...etc etc

You can go deeper in the content tree like specifying the type of sms, like: content://sms/sent or content://sms/inbox and add projection and selection for the second argument of the query() method like, name, person, status of the message (like the Calls example).

This requires permission:

<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
无法言说的痛 2024-12-14 13:39:23

因为我没有足够的声誉来回答@Prasad 问题评论关于

错误 -> new Runnable(){} 类型的 getContentResolver() 未定义

getContentResolver() 是应用程序上下文的一部分,因此,如果您使用 BroadcastReceiver,请在 onReceive() 中使用上下文 像这样的函数

@Override
public void onReceive(Context context, Intent intent) {
    context.getContentResolver()
}

如果您在 Activity 中使用上面的代码,那么您可以使用

getApplicationContext().getContentResolver()

并确保使用 [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 in onReceive() function like this

@Override
public void onReceive(Context context, Intent intent) {
    context.getContentResolver()
}

If you are using the code above inside an Activity, then you can use

getApplicationContext().getContentResolver()

also make sure to use [Ctrl + Shift + O (O not zero)] to organize imports

Key Shortcut for Eclipse Imports

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文