Android 如何在从广播接收器接收的列表中存储消息
我正在开发一个与通过广播接收器接收消息相关的代码 基于休闲代码Android 如何在从广播接收器接收的列表中存储消息,android,sms,broadcastreceiver,Android,Sms,Broadcastreceiver,我正在开发一个与通过广播接收器接收消息相关的代码 基于休闲代码 @Override public void onReceive(Context context, Intent intent) { //---get the SMS message passed in--- Bundle bundle = intent.getExtras(); SmsMessage[] msgs = null; St
@Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
@覆盖
公共void onReceive(上下文、意图)
{
//---获取传入的SMS消息---
Bundle=intent.getExtras();
SmsMessage[]msgs=null;
字符串str=“”;
if(bundle!=null)
{
//---检索收到的SMS消息---
Object[]pdus=(Object[])bundle.get(“pdus”);
msgs=新SMS消息[PDU.length];
对于(int i=0;i有两种可能的方法:将其存储在数据库或文件中。我认为,更好的方法是使用SQLite。文档如何创建简单的数据库,存储值,并在必要时接收。还有文档如何读取和写入文件。您的问题不清楚,您在其中的哪一部分实际面临问题?在toast i displant中ay获取消息。我想将该消息存储在列表中。然后重试。toast是否能够显示它/我不想按toast显示消息。我想按列表显示消息(如果我将所有消息存储在列表中)。然后,只要您想在列表中显示消息,就需要收集字符串数组中的所有消息,并创建一个列表,适配器将字符串数组作为参数传递给您。