Android-如何在活动运行时将最新短信添加到对话列表中?
我正在开发一个短信聊天应用程序。我想知道当活动像股票短信应用程序或任何其他信使一样打开时,如何在列表视图的底部添加最新的短信(发送或接收) 我尝试使用ContentObserver。只要SMS数据库中有一些更改,我就尝试用新的数据集重新加载ListView,但它不起作用。每次看到一个新的短信(发送或接收),我必须重新启动应用程序,以便列表可以重新加载。我希望这些新的信息显示在聊天窗口,只要我发送或接收他们就像我们聊天的方式,通过gtalk,msn或任何短信应用程序 这是我的密码:-Android-如何在活动运行时将最新短信添加到对话列表中?,android,sms,chat,contentobserver,Android,Sms,Chat,Contentobserver,我正在开发一个短信聊天应用程序。我想知道当活动像股票短信应用程序或任何其他信使一样打开时,如何在列表视图的底部添加最新的短信(发送或接收) 我尝试使用ContentObserver。只要SMS数据库中有一些更改,我就尝试用新的数据集重新加载ListView,但它不起作用。每次看到一个新的短信(发送或接收),我必须重新启动应用程序,以便列表可以重新加载。我希望这些新的信息显示在聊天窗口,只要我发送或接收他们就像我们聊天的方式,通过gtalk,msn或任何短信应用程序 这是我的密码:- public
public class ThreadData extends ListActivity
{
private static final Uri SMS_URI = Uri.parse("content://sms");
HashMap<Long, BodyType> bodies = new HashMap<Long, BodyType>();
private String name, number;
private ListView listView;
private Activity activity;
ThreadDataAdapter adapter;
Handler handler;
ArrayList<BodyType> items;
private Context context;
private static final String PHONE_NUMBER_SEPARATORS = " ()-./";
static HashMap<String, ContactInfo.ContactDetail> info = new HashMap<String, ContactInfo.ContactDetail>();
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.chats);
Bundle extras = getIntent().getExtras();
activity = this;
context = this;
listView = getListView();
if(extras != null)
{
number = extras.getString("address");
ContactInfo.ContactDetail nameInfo = getContactsDetailWithNumber(number);
if(nameInfo != null)
{
name = nameInfo.name;
}
else
{
name = number;
}
}
TextView person = (TextView) findViewById(R.id.chat_person);
person.setText(name);
ImageButton callPerson = (ImageButton) findViewById(R.id.call_person);
callPerson.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
popUpDialerAlert();
}
});
buildMessageList();
items = sortBodies(bodies);
adapter = new ThreadDataAdapter(this, items);
listView.setAdapter(adapter);
listView.setStackFromBottom(true);
getContentResolver().registerContentObserver(SMS_URI, true, new MyContentObserver(handler));
//Intent intent = new Intent(this, RBSMSService.class);
//startService(intent);
}
public void loadListView()
{
items.clear();
items = sortBodies(bodies);
adapter = new ThreadDataAdapter(this, items);
listView.setAdapter(adapter);
listView.setStackFromBottom(true);
}
public void buildMessageList()
{
Cursor cursor = getContentResolver().query(SMS_URI, null, null, null, "date ASC");
startManagingCursor(cursor);
while (cursor.moveToNext())
{
BodyType bodyInfo = new BodyType();
String address = cursor.getString(cursor.getColumnIndex("address"));
bodyInfo.body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
Long date = cursor.getLong(cursor.getColumnIndexOrThrow("date"));
bodyInfo.date = date;
String type = cursor.getString(cursor.getColumnIndexOrThrow("type"));
if(type.equals("1"))
{
bodyInfo.type = "received";
}
else if(type.equals("2"))
{
bodyInfo.type = "sent";
}
else if(type.equals("3"))
{
bodyInfo.type = "draft";
}
String number = filterPhoneNumber(address);
ContactInfo.ContactDetail nameInfo = getContactsDetailWithNumber(number);
String personName = number;
if(nameInfo != null)
{
personName = nameInfo.name;
}
else
{
personName = number;
}
if(personName.equals(name))
{
bodies.put(date, bodyInfo);
}
}
}
public void popUpDialerAlert()
{
AlertDialog.Builder dialerAlert = new AlertDialog.Builder(this);
dialerAlert.setTitle("Confirm");
dialerAlert.setMessage("Call" + " " + name + "?");
dialerAlert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
Uri uri = Uri.parse("tel:" + number);
Intent intent = new Intent((Intent.ACTION_CALL));
intent.setData(uri);
context.startActivity(intent);
}
});
dialerAlert.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
}
});
dialerAlert.show();
}
public ArrayList<BodyType> sortBodies(HashMap<Long, BodyType> bodies)
{
ArrayList<Long> dates = new ArrayList<Long>(bodies.keySet());
Collections.sort(dates);
ArrayList<BodyType> items = new ArrayList<BodyType>();
for(Long date : dates)
{
for(Long key : bodies.keySet())
{
if(date.equals(key))
{
items.add(bodies.get(key));
}
}
}
return items;
}
public void printBodies(ArrayList<BodyType> items)
{
for(BodyType bodyType : items)
{
log(bodyType.date.toString());
log(bodyType.body);
log(bodyType.type);
}
}
static class BodyType
{
public String type;
public String body;
public Long date;
}
public static ContactInfo.ContactDetail getContactsDetailWithNumber(String number)
{
if(info.containsKey(number))
{
return info.get(number);
}
return null;
}
public static String filterPhoneNumber(String phoneNumber)
{
if (phoneNumber == null)
{
return null;
}
int length = phoneNumber.length();
StringBuilder builder = new StringBuilder(length);
for (int i = 0; i < length; i++)
{
char character = phoneNumber.charAt(i);
if (PHONE_NUMBER_SEPARATORS.indexOf(character) == -1)
{
builder.append(character);
}
}
return builder.toString();
}
public class MyContentObserver extends ContentObserver
{
public MyContentObserver(Handler handler)
{
super(handler);
}
@Override
public void onChange(boolean selfChange)
{
bodies.clear();
buildMessageList();
items = sortBodies(bodies);
runOnUiThread(new Runnable() {
public void run() {
loadListView();
}
});
super.onChange(selfChange);
}
}
public void log(String msg)
{
Log.e("ThreadData", msg);
}
}
公共类ThreadData扩展ListActivity
{
私有静态最终Uri SMS_Uri=Uri.parse(“content://sms");
HashMap body=新的HashMap();
私有字符串名称、编号;
私有列表视图列表视图;
私人活动;
螺纹数据适配器;
处理者;
数组列表项;
私人语境;
专用静态最终字符串电话号码分隔符=“()-。/”;
静态HashMap info=newhashmap();
创建公共void(Bundle)
{
super.onCreate(bundle);
setContentView(R.layout.chats);
Bundle extras=getIntent().getExtras();
活动=此;
上下文=这个;
listView=getListView();
如果(附加值!=null)
{
number=extras.getString(“地址”);
ContactInfo.ContactDetail nameInfo=getContactsDetailWithNumber(编号);
如果(nameInfo!=null)
{
name=nameInfo.name;
}
其他的
{
姓名=编号;
}
}
TextView person=(TextView)findViewById(R.id.chat\u person);
person.setText(姓名);
ImageButton callPerson=(ImageButton)findViewById(R.id.call\u person);
setOnClickListener(新的OnClickListener(){
公共void onClick(视图){
popUpDialerAlert();
}
});
buildMessageList();
项目=分类体(实体);
适配器=新的ThreadDataAdapter(此,项);
setAdapter(适配器);
listView.setStackFromBottom(true);
getContentResolver().registerContentObserver(SMS_URI,true,新MyContentObserver(处理程序));
//Intent Intent=新Intent(这个,RBSMSService.class);
//startService(意向);
}
public void loadListView()
{
items.clear();
项目=分类体(实体);
适配器=新的ThreadDataAdapter(此,项);
setAdapter(适配器);
listView.setStackFromBottom(true);
}
public void buildMessageList()
{
Cursor Cursor=getContentResolver().query(SMS_URI,null,null,null,“日期ASC”);
开始管理游标(游标);
while(cursor.moveToNext())
{
BodyType bodyInfo=新的BodyType();
字符串地址=cursor.getString(cursor.getColumnIndex(“地址”);
bodyInfo.body=cursor.getString(cursor.getColumnIndexOrThrow(“body”);
Long date=cursor.getLong(cursor.getColumnIndexOrThrow(“日期”));
bodyInfo.date=日期;
字符串类型=cursor.getString(cursor.getColumnIndexOrThrow(“类型”);
if(类型等于(“1”))
{
bodyInfo.type=“已接收”;
}
else if(类型等于(“2”))
{
bodyInfo.type=“已发送”;
}
else if(类型等于(“3”))
{
bodyInfo.type=“草稿”;
}
字符串编号=filterPhoneNumber(地址);
ContactInfo.ContactDetail nameInfo=getContactsDetailWithNumber(编号);
字符串personName=number;
如果(nameInfo!=null)
{
personName=nameInfo.name;
}
其他的
{
人名=号码;
}
if(personName.equals(name))
{
body.put(日期、bodyInfo);
}
}
}
public void popUpDialerAlert()
{
AlertDialog.Builder dialerert=新建AlertDialog.Builder(此);
dialerAlert.setTitle(“确认”);
setMessage(“调用“+”+名称+”?”;
dialerAlert.setPositiveButton(“是”,新的DialogInterface.OnClickListener(){
公共void onClick(DialogInterface,inti){
Uri=Uri.parse(“电话:”+number);
意图=新意图((Intent.ACTION_CALL));
intent.setData(uri);
背景。开始触觉(意图);
}
});
dialerert.setNegativeButton(“否”,新的DialogInterface.OnClickListener(){
公共void onClick(DialogInterface,inti){
}
});
dialerert.show();
}
公共ArrayList sortBodies(哈希映射体)
{
ArrayList日期=新的ArrayList(body.keySet());
集合。排序(日期);
ArrayList items=新建ArrayList();
对于(长日期:日期)
{
for(长键:bodys.keySet())
{
如果(日期等于(关键))
{
items.add(body.get(key));
}
}
}
退货项目;
}
公共作废打印体(ArrayList项)
{
用于(车身类型车身类型:项目)
{
日志(bodyType.date.toString());
日志(bodyType.body);
日志(bodyType.type);
}
}
静态类车身类型
{
公共字符串类型;
公共机构;
公众长期约会;
}
public static ContactInfo.ContactDetail getContactsDetailWithNumber(字符串编号)
{
if(信息容器(编号))
{
退货信息获取(编号);
}
返回null;
}
公共静态字符串过滤器phoneNumber(字符串电话号码)
{
if(phoneNumber==null)
{
返回null;
}
int length=phoneNumber.length();
StringBuilder=新的StringBuilder(长度);
for(int i=0;i