android AppWidget:短信查询适用于模拟器,但不适用于真实设备

发布于 2024-12-10 08:49:02 字数 1160 浏览 1 评论 0原文

我构建了一个 appWidget,当收到新短信时,它会更新特定联系人的未读短信计数,为此我注册了一个 contentObserver 来监视传入短信。

mySMSObserver 类调用以下方法来获取特定联系人的未读消息数(通过他的 ID)。

到目前为止一切顺利,问题是下面的查询在模拟器上运行良好,但是当我在 Android 设备上尝试时,它总是返回 0 条未读消息(如 Toast 所示)。

private static String updateUnreadSMS(String contactID, Context context) {

ContentResolver cr=context.getContentResolver();
//final Uri SMS_INBOX=(Uri.parse("content://mms-sms/conversations"));
//final Uri SMS_INBOX=(Uri.parse("content://mms/inbox"));
final Uri SMS_INBOX=Uri.parse("content://sms/inbox");

Cursor c = cr.query(SMS_INBOX, null, "read = 0 AND person=?", new String[]{contactID}, null);

// get number of unread sms messages
int unreadMessagesCount = c.getCount();

Log.d("DEBUG", "UnreadSMS: "+unreadMessagesCount);
Toast.makeText(context, "updateUnreadSMS "+unreadMessagesCount, Toast.LENGTH_LONG).show();

c.deactivate();
return String.valueOf(unreadMessagesCount); 

}

不同设备需要不同的查询吗?

如何为“content://mms/inbox”编写相同的查询? ,因为“人=?”是该查询的非法字段。

我们很高兴为您解决此问题提供帮助和建议:)

I have built an appWidget which update it's unread sms count for specific contact when a new sms comes in, for that I've registered a contentObserver to monitor incoming sms.

The mySMSObserver class calls the method below for getting the number of unread messages for specific contact, by his ID.

So far so good, the problem is that the query below works fine on the emulator, but when I try that on my android device it allways return 0 unread messages (as shown by Toast).

private static String updateUnreadSMS(String contactID, Context context) {

ContentResolver cr=context.getContentResolver();
//final Uri SMS_INBOX=(Uri.parse("content://mms-sms/conversations"));
//final Uri SMS_INBOX=(Uri.parse("content://mms/inbox"));
final Uri SMS_INBOX=Uri.parse("content://sms/inbox");

Cursor c = cr.query(SMS_INBOX, null, "read = 0 AND person=?", new String[]{contactID}, null);

// get number of unread sms messages
int unreadMessagesCount = c.getCount();

Log.d("DEBUG", "UnreadSMS: "+unreadMessagesCount);
Toast.makeText(context, "updateUnreadSMS "+unreadMessagesCount, Toast.LENGTH_LONG).show();

c.deactivate();
return String.valueOf(unreadMessagesCount); 

}

Is there different query's needed for different devices?

how do I write the same query for "content://mms/inbox"?, Because "person=?" is an illegal field for that query.

Will be glad for your help and advice for solving this problem :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

懒猫 2024-12-17 08:49:02

直接访问数据库可能不是最好的主意。因为位置和数据结构可能会随着不同的 Android 版本而变化(从 7 => 8 开始的日历存储就是如此)。不过,请检查您的设备位置和数据库结构是否相同。

对于彩信,您可以查看 PduParser 用于解析该文件夹中的彩信和其他类。

Accessing the databases directly might not be the best idea. As locations and data structure might change with different Android versions (It did so with the Calendar storage from 7 => 8). Nevertheless check if your device location and database structure is the same.

For MMS you might take a peek at PduParser used to parse MMS messages and other classes in this folder.

新人笑 2024-12-17 08:49:02

为了获取未读短信,我使用以下代码:

final Cursor cursorSMS = context.getContentResolver().query(Uri.parse("content://sms/inbox"), null, "read = 0", null, null);
if (ContextCompat.checkSelfPermission(context.getApplicationContext(), android.Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED) {
   if (cursorSMS != null) nbMsgUnread = cursorSMS.getCount();
}

在模拟器上没有问题。
但在真实手机上,系统不会立即实现通过 cursorSMS.getCount(); 获取的计数。
当收到短信时,BroadcastReceiver 太快,并且 count = 0。
接收短信时,您的广播接收器必须启动警报以区分 .getCount();
几秒钟后(在我的例子中是 5 秒),系统会实际显示未读短信计数。
问候。

To get unread SMS, I use this code:

final Cursor cursorSMS = context.getContentResolver().query(Uri.parse("content://sms/inbox"), null, "read = 0", null, null);
if (ContextCompat.checkSelfPermission(context.getApplicationContext(), android.Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED) {
   if (cursorSMS != null) nbMsgUnread = cursorSMS.getCount();
}

No problem on the emulator.
But on a real phone, count get by cursorSMS.getCount(); is not actualised immediately by the system.
When incoming SMS, BroadcastReceiver is too fast, and count = 0.
When receiving a SMS, Your broadcastReceiver must launch an alarm to differ .getCount();.
After few seconds (5sec in my case), unread SMS count is actualised by the system.
Regards.

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