android:
id="@+id/body"
android:
layout_width="fill_parent"
android:
layout_height="wrap_content"
android:
paddingLeft="10dip"
/>
3.鉴于SMSSent与SMSInbox大同小异故不再细说仅补上代码
publicclassSMSSentextendsActivity{
ListViewlv;
Cursorcursor;
ItemAdapteradapter;
SMSObserversObserver;
Handlerhandler;
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
setTitle(R.string.sent);
lv=(ListView)findViewById(R.id.list);
cursor=getContentResolver().query(Uri.parse("content:
//sms/sent"),null,null,null,null);
adapter=newItemAdapter(this);
lv.setAdapter(adapter);
lv.setOnItemClickListener(newOnItemClickListener(){
@Override
publicvoidonItemClick(AdapterView
>arg0,Viewarg1,intarg2,
longarg3){
//TODOAuto-generatedmethodstub
cursor.moveToPosition(arg2);
Stringbody=cursor.getString(cursor.getColumnIndexOrThrow("body")).toString();
Stringaddress=cursor.getString(cursor.getColumnIndexOrThrow("address")).toString();
Bundleb=newBundle();
b.putString("body",body);
b.putString("address",address);
Intentintent=newIntent(SMSSent.this,SMSDetails.class);
intent.putExtras(b);
startActivity(intent);
}
});
//registerSMSObserve
handler=newHandler(){
publicvoidhandleMessage(Messagemsg){
if(msg.what==SMSObserver.SMS_CHANGE){
cursor=getContentResolver().query(Uri.parse("content:
//sms/sent"),null,null,null,null);
adapter.notifyDataSetChanged();
}
}
};
sObserver=newSMSObserver(handler);
this.getContentResolver().registerContentObserver(Uri.parse("content:
//sms"),true,sObserver);
}
publicclassItemAdapterextendsBaseAdapter{
Activityactivity;
publicItemAdapter(Activitya){
activity=a;
}
@Override
publicintgetCount(){
//TODOAuto-generatedmethodstub
returncursor.getCount();
}
@Override
publicObjectgetItem(intarg0){
//TODOAuto-generatedmethodstub
returncursor.getString(arg0);
}
@Override
publiclonggetItemId(intarg0){
//TODOAuto-generatedmethodstub
returnarg0;
}
@Override
publicViewgetView(intarg0,Viewarg1,ViewGrouparg2){
//TODOAuto-generatedmethodstub
returncomposeItem(arg0);
}
privateViewcomposeItem(intposition){
cursor.moveToPosition(position);
Stringbody=cursor.getString(cursor.getColumnIndexOrThrow("body")).toString();
Stringnumber=cursor.getString(cursor.getColumnIndexOrThrow("address")).toString();
LinearLayoutitem=(LinearLayout)activity.getLayoutInflater().inflate(R.layout.item,null);
LinearLayoutl1=(LinearLayout)item.getChildAt(0);
TextViewtbody=(TextView)item.getChildAt
(1);
if(tbody!
=null){
tbody.setText(body);
}
TextViewtnum=(TextView)l1.getChildAt
(1);
if(tnum!
=null){
tnum.setText(number);
}
ImageViewimage=(ImageView)l1.getChildAt(0);
image.setImageResource(R.drawable.message);
returnitem;
}
}
}
4.emulator运行截图
>4.1. SMSInbox:
-4.1.1.通过telnetlocalhost5554登录emulator 通过smssend123helloto123模拟发送短信
-4.1.2.短信发送记录为:
AndroidConsole:
type'help'foralistofcommands
OK
smssend12ds
OK
smssend23hito23
OK
smssend34iamgriddinshi
OK
-4.1.3.SMSInbox:
>4.2.SMSSent
-4.2.1.已发短信记录:
-4.2.2.SMSSent:
5.未解决问题:
>5.1.ContentObserver只能监听content:
//sms 而不支持content:
//sms/inboxcontent:
//sms/sent 个人猜测是因为:
android在写sms数据库insert(...)没有通过ContentResolver通知content:
//sms/inboxcontent:
//sms/sent所致即:
没有以下代码:
getContext().getContentResolver().notifyChange(no