SMS管理Word文件下载.docx
《SMS管理Word文件下载.docx》由会员分享,可在线阅读,更多相关《SMS管理Word文件下载.docx(17页珍藏版)》请在冰点文库上搜索。
![SMS管理Word文件下载.docx](https://file1.bingdoc.com/fileroot1/2023-5/10/e7ec4493-42a3-44a8-8ec1-e847ffda121c/e7ec4493-42a3-44a8-8ec1-e847ffda121c1.gif)
1.定义SMSObserver用于监听目标并通过Handle通知注册方
publicclassSMSObserverextendsContentObserver{
publicfinalstaticintSMS_CHANGE=0;
Handlerhandle;
publicSMSObserver(Handlerh){
super(h);
//TODOAuto-generatedconstructorstub
handle=h;
}
publicvoidonChange(booleanselfChange){
//TODOAuto-generatedmethodstub
super.onChange(selfChange);
//notifySMSInbox&
SMSSent
handle.sendEmptyMessage(SMS_CHANGE);
}
2.定义注册方:
SMSInbox鉴于SMSSent与其原理类似故打算以SMSInbox为例
>
2.1.显示当前所有收信箱并与ListView适配
lv=(ListView)findViewById(R.id.list);
cursor=getContentResolver().query(Uri.parse("
//sms/inbox"
),null,null,null,null);
adapter=newItemAdapter(this);
lv.setAdapter(adapter);
2.2.定义Handle用于接受变动并注册与ContentObserve当接到通知后查询目标Uri并刷新显示
handler=newHandler(){
publicvoidhandleMessage(Messagemsg){
if(msg.what==SMSObserver.SMS_CHANGE){
cursor=getContentResolver().query(Uri.parse("
adapter.notifyDataSetChanged();
}
}
};
sObserver=newSMSObserver(handler);
this.getContentResolver().registerContentObserver(Uri.parse("
//sms"
),true,sObserver);
2.3.
SMSInbox仅用于显示收信箱
故定义SMSDetailsextendsActivity用于详细显示sms信息
-2.3.1.定义布局:
details.xml
<
?
xmlversion="
1.0"
encoding="
utf-8"
LinearLayoutxmlns:
android="
android:
orientation="
vertical"
layout_width="
fill_parent"
layout_height="
>
TextView
id="
@+id/detailsNumber"
wrap_content"
/>
@+id/detailsBody"
200dip"
horizontal"
300dip"
Button
@+id/detailsReply"
100dip"
layout_gravity="
left"
text="
回复"
@+id/detailsOK"
right"
确定"
/LinearLayout>
-2.3.2.其中2个TextView分别显示信息地址和正文2个Button一个用于关闭当前窗口一个用于短信回复且自动填充收信人地址
publicclassSMSDetailsextendsActivity{
TextViewtextNumber,textBody;
ButtonbtnOK,btnReply;
OnClickListenercl;
Stringaddress,body;
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
textNumber=(TextView)findViewById(R.id.detailsNumber);
textBody=(TextView)findViewById(R.id.detailsBody);
btnReply=(Button)findViewById(R.id.detailsReply);
btnOK=(Button)findViewById(R.id.detailsOK);
Intenti=this.getIntent();
Bundlebundle=i.getExtras();
address=bundle.getString("
address"
);
body=bundle.getString("
body"
textNumber.setText("
from:
"
+address);
textBody.setText("
messagebody:
\n"
+body);
cl=newOnClickListener(){
@Override
publicvoidonClick(Viewarg0){
//TODOAuto-generatedmethodstub
switch(arg0.getId()){
caseR.id.detailsReply:
sendGoNativeNew(address);
break;
caseR.id.detailsOK:
sendBack();
}
btnReply.setOnClickListener(cl);
btnOK.setOnClickListener(cl);
publicvoidsendGoNativeNew(Stringaddress){
//nativesendsmsapp
IntentsendIntent=newIntent(Intent.ACTION_SENDTO,Uri.parse("
sms:
//"
));
//autofill"
sendIntent.putExtra("
address);
startActivity(sendIntent);
publicvoidsendBack(){
Intenti=newIntent();
this.setResult(RESULT_OK,i);
this.finish();
-2.3.3.
点击SMSInbox某项跳转到SMSDetails
lv.setOnItemClickListener(newOnItemClickListener(){
publicvoidonItemClick(AdapterView<
arg0,Viewarg1,intarg2,
longarg3){
cursor.moveToPosition(arg2);
Stringbody=cursor.getString(cursor.getColumnIndexOrThrow("
)).toString();
Stringaddress=cursor.getString(cursor.getColumnIndexOrThrow("
Bundleb=newBundle();
b.putString("
body);
Intentintent=newIntent(SMSInbox.this,SMSDetails.class);
intent.putExtras(b);
startActivity(intent);
});
-2.3.4.其中
item.xml用于定义子项布局
ImageView
@+id/body"
@+id/num"
paddingLeft="
5dip"
10dip"
3.鉴于SMSSent与SMSInbox大同小异故不再细说仅补上代码
publicclassSMSSentextendsActivity{
ListViewlv;
Cursorcursor;
ItemAdapteradapter;
SMSObserversObserver;
Handlerhandler;
setContentView(R.layout.list);
setTitle(R.string.sent);
lv=(ListView)findViewById(R.id.list);
//sms/sent"
lv.setOnItemClickListener(newOnItemClickListener(){
Intentintent=newIntent(SMSSent.this,SMSDetails.class);
//registerSMSObserve
handler=newHandler(){
publicclassItemAdapterextendsBaseAdapter{
Activityactivity;
publicItemAdapter(Activitya){
activity=a;
@Override
publicintgetCount(){
//TODOAuto-generatedmethodstub
returncursor.getCount();
publicObjectgetItem(intarg0){
returncursor.getString(arg0);
publiclonggetItemId(intarg0){
returnarg0;
publicViewgetView(intarg0,Viewarg1,ViewGrouparg2){
returncomposeItem(arg0);
privateViewcomposeItem(intposition){
cursor.moveToPosition(position);
Stringbody=cursor.getString(cursor.getColumnIndexOrThrow("
Stringnumber=cursor.getString(cursor.getColumnIndexOrThrow("
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!
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
smssend23hito23
smssend34iamgriddinshi
-4.1.3.SMSInbox:
4.2.SMSSent
-4.2.1.已发短信记录:
-4.2.2.SMSSent:
5.未解决问题:
5.1.ContentObserver只能监听content:
//sms
而不支持content:
//sms/sent
个人猜测是因为:
android在写sms数据库insert(...)没有通过ContentResolver通知content:
//sms/sent所致即:
没有以下代码:
getContext().getContentResolver().notifyChange(no