Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将收到的SMS附加到ListView_Android_Sms_Broadcastreceiver - Fatal编程技术网

Android 如何将收到的SMS附加到ListView

Android 如何将收到的SMS附加到ListView,android,sms,broadcastreceiver,Android,Sms,Broadcastreceiver,我有以下代码来显示在listview中收到的消息: package com.example.smsTest; import java.util.ArrayList; import android.app.ListActivity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.Inte

我有以下代码来显示在listview中收到的消息:

package com.example.smsTest;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SMSReceiverActivity extends ListActivity {
    private BroadcastReceiver mIntentReceiver;
    ListView listview;
    ArrayAdapter<String> arrayAdpt;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smsreceiver);

        listview=this.getListView();
    }

    @Override
    protected void onResume() {
        super.onResume();

        IntentFilter intentFilter = new IntentFilter("SmsMessage.intent.MAIN");
        mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String msg = intent.getStringExtra("get_msg");

            //Process the sms format and extract body &amp; phoneNumber
            msg = msg.replace("\n", "");
            String body = msg.substring(msg.lastIndexOf(":")+1, msg.length());
            String pNumber = msg.substring(0,msg.lastIndexOf(":"));

            //Add it to the list or do whatever you wish to
            ArrayList<String> bodyarr=new ArrayList<String>();
            bodyarr.add(body);
            arrayAdpt = new ArrayAdapter<String>(SMSReceiverActivity.this, android.R.layout.simple_list_item_1,
            bodyarr);           
            listview.setAdapter(arrayAdpt);
            arrayAdpt.notifyDataSetChanged();
        }
    };

    this.registerReceiver(mIntentReceiver, intentFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        this.unregisterReceiver(this.mIntentReceiver);
    }

}
package com.example.smsTest;
导入java.util.ArrayList;
导入android.app.ListActivity;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.content.IntentFilter;
导入android.os.Bundle;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
公共类SMSReceiverActivity扩展了ListActivity{
专用广播接收机;
列表视图列表视图;
ArrayAdapter arrayAdpt;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u smsreceiver);
listview=this.getListView();
}
@凌驾
受保护的void onResume(){
super.onResume();
IntentFilter IntentFilter=新的IntentFilter(“SmsMessage.intent.MAIN”);
mIntentReceiver=新广播接收器(){
@凌驾
公共void onReceive(上下文、意图){
String msg=intent.getStringExtra(“get_msg”);
//处理sms格式并提取正文和电话号码
msg=msg.replace(“\n”,”);
String body=msg.substring(msg.lastIndexOf(“:”)+1,msg.length());
字符串pNumber=msg.substring(0,msg.lastIndexOf(“:”);
//将它添加到列表中,或者做任何你想做的事情
ArrayList bodyarr=新的ArrayList();
bodyarr.添加(body);
arrayAdpt=新的ArrayAdapter(SMSReceiverActivity.this,android.R.layout.simple\u list\u item\u 1,
bodyarr);
setAdapter(arrayAdpt);
arrayAdpt.notifyDataSetChanged();
}
};
此注册表接收程序(mIntentReceiver、intentFilter);
}
@凌驾
受保护的void onPause(){
super.onPause();
this.unregistereceptor(this.minentreceiver);
}
}
但是,问题是上一条消息被覆盖。我尝试添加
arrayAdpt.notifyDataSetChanged()代码无效

我在这里也读了很多答案,但它对我的代码不起作用


请提供帮助。

每次收到新消息时,您都会创建一个全新的空列表,这就是为什么以前的消息总是被覆盖的原因

相反,将bodyarr和arrayAdpt的声明移动到类字段,以便在每次收到新消息时共享和修改它们:

private ArrayList<String> bodyarr = new ArrayList<String>();
private ArrayAdapter<String> arrayAdpt;

没问题,很高兴我能帮忙!
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smsreceiver);

        listview = this.getListView();
        arrayAdpt = new ArrayAdapter<String>(SMSReceiverActivity.this, android.R.layout.simple_list_item_1,
        bodyarr);

        listview.setAdapter(arrayAdpt);
    }
mIntentReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String msg = intent.getStringExtra("get_msg");

        //Process the sms format and extract body &amp; phoneNumber
        msg = msg.replace("\n", "");
        String body = msg.substring(msg.lastIndexOf(":")+1, msg.length());
        String pNumber = msg.substring(0,msg.lastIndexOf(":"));

        bodyarr.add(body);

        arrayAdpt.notifyDataSetChanged();
    }