在Android数据库收件箱中选择第一个SMS

在Android数据库收件箱中选择第一个SMS,第1张

概述我迫切希望找到解决方案,所以我请求帮助!我是一名新的法国程序员.我的目标是创建一个能够显示SMS的小部件.我的问题是我不知道如何创建一个光标来选择内容中的第一个短信://sms/inbox请原谅我糟糕的英语,我希望你能理解我的英语.谢谢您的回答.这是我的代码:packagesfeir.monwid

我迫切希望找到解决方案,所以我请求帮助!
我是一名新的法国程序员.我的目标是创建一个能够显示SMS的小部件.
我的问题是我不知道如何创建一个光标来选择内容中的第一个短信:// sms / inBox
请原谅我糟糕的英语,我希望你能理解我的英语.
谢谢您的回答.
这是我的代码:

package sfeir.monWidget;import androID.R.string;import androID.appWidget.AppWidgetManager;import androID.appWidget.appwidgetprovider;import androID.net.Uri;import androID.Widget.RemoteVIEws;import androID.content.Componentname;import androID.content.ContentResolver;import androID.content.Context;import androID.content.Intent;import androID.database.Cursor;import androID.Widget.ArrayAdapter;   public class MonWidget extends appwidgetprovider { public voID onUpdate(Context context, AppWidgetManager appWidgetManager,            int[] appWidgetIDs) {     Uri uri = Uri.parse("content://sms/inBox");    // returns all the results.    Cursor c= getContentResolver().query(uri, null, null ,null,null);     // called by the Activity.    startManagingCursor(c);    String body = null;    String number = null;    if(c.movetoFirst()) { // move cursor to first row       // retrIEves the body and number of the SMS       body = c.getString(c.getColumnIndexOrThrow("body")).toString();       number = c.getString(c.getColumnIndexOrThrow("address")).toString();    }    // when your done, close the cursor.    c.close();  RemoteVIEws updateVIEws = new RemoteVIEws(context.getPackagename(),         R.layout.Widget_layout); updateVIEws.setTextcolor(R.ID.text, 0xFF000000); updateVIEws.setTextVIEwText(R.ID.text, (CharSequence) body); Componentname thisWidget = new Componentname(context, MonWidget.class); appWidgetManager.updateAppWidget(thisWidget, updateVIEws); }

}

解决方法:

您需要设置特定权限(请参阅下面的链接),但这里是使用Cursor检索第一条SMS消息的代码示例.

Uri uri = Uri.parse("content://sms/inBox");// returns all the results from the given ContextCursor c = context.getContentResolver().query(uri, null, null ,null,null); String body = null;String number = null;if(c.movetoFirst()) { // move cursor to first row   // retrIEves the body and number of the SMS   body = c.getString(c.getColumnIndexOrThrow("body")).toString();   number = c.getString(c.getColumnIndexOrThrow("address")).toString();}// when your done, close the cursor.c.close(); 

我建议查看FrontPage/Tutorials/SMS Messaging – Mobdev Wiki,它提供了一个很好的介绍处理AndroID上的短信处理.

编辑:

这些方法对您的应用程序不可见,因为它没有扩展到Activity超类.默认情况下,在开发应用程序时,它会从该关系继承方法.但是你本身并没有创建一个应用程序,而是在开发一个小部件.

幸运的是,在onUpdate方法中,它们传递当前Context,这是Activity的超类,因此我们可以使用变量context来调用getContentResolver(参见上面的代码)

我还从代码中删除了startManagingCursor方法,它不是完全必需的,它允许Activity根据Activity的生命周期处理给定的Cursor的生命周期.

如果有任何问题,请告诉我.

编辑2:

在AndroIDManifest.xml文件中,您需要设置正确的权限以避免任何异常,请添加此行.

<uses-permission androID:name="androID.permission.READ_SMS"></uses-permission>
总结

以上是内存溢出为你收集整理的在Android数据库收件箱中选择第一个SMS全部内容,希望文章能够帮你解决在Android数据库收件箱中选择第一个SMS所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1109106.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存