2013-04-07 2 views
0

에 수신 된 SMS를 추가하려면 어떻게 목록보기에서받은 메시지를 표시하려면 다음 코드가 있습니다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); 
    } 

} 

그러나, 문제는 덮어 쓰기 이전의 메시지입니다. 나는 쓸모없는 arrayAdpt.notifyDataSetChanged(); 코드를 추가하려고했다.

여기도 많은 답변을 읽었지만 내 코드에서는 작동하지 않습니다.

도움주세요.

답변

2

새 메시지를받을 때마다 공란 목록이 새로 생성되므로 이전 메시지는 항상 덮어 쓰게됩니다.

대신, 클래스 필드에 bodyarr 및 arrayAdpt 당신의 선언을 이동 그들이 공유하고 새로운 메시지가 수신 될 때마다 수정 될 수 있도록 :

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

에서 onCreate에서를, 목록 어댑터를 설정해야합니다 당신의 ListView에 :

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); 
    } 

그런 다음, 방송 수신기에 대한 onReceive 방법, 당신은 거의 정확하게 당신이 당신이 이미 추가 한 것을, 그래서 기존 목록과 배열 어댑터를 사용하는 것을 제외하고, 전에하던 일을 할 수있는 것 덮어 쓰지 않는다 :

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(); 
    } 
+0

놀라운! 고마워요 ... 매력처럼 작동 ... – jaypabs

+0

아무 문제 없어, 다행스럽게 도울 수있어! – mattgmg1990