SMS管理.docx

上传人:b****4 文档编号:11590402 上传时间:2023-03-19 格式:DOCX 页数:17 大小:57.25KB
下载 相关 举报
SMS管理.docx_第1页
第1页 / 共17页
SMS管理.docx_第2页
第2页 / 共17页
SMS管理.docx_第3页
第3页 / 共17页
SMS管理.docx_第4页
第4页 / 共17页
SMS管理.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

SMS管理.docx

《SMS管理.docx》由会员分享,可在线阅读,更多相关《SMS管理.docx(17页珍藏版)》请在冰豆网上搜索。

SMS管理.docx

SMS管理

SMS管理

[功能]

 

1.收信箱:

显示所有收到的信息且实时显示即:

当有新信息收到能自动刷新显示

 

2.发信箱:

显示所有已发信息同上

 

3.编写新信息:

鉴于一些问题打算不自行定义而只通过Intent调用系统的

[原理]

 

1.通过目标Uri显示收信箱发信箱 目标Uri:

content:

//sms/inboxcontent:

//sms/sent

2.实时刷新:

一个办法是开辟thread定时查询目标Uri显示之 但会带来一些效能影响所以决定使用ContentObserve监听目标Uri当有变动由ContentObserve通知注册方该Uri:

content:

//sms

 

3.注意:

ContentObserve不能监听:

content:

//sms/inbox&content:

//sms/sent而只能监听content:

//sms

 

 

[代码步骤]

 

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("content:

//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("content:

//sms/inbox"),null,null,null,null);

adapter.notifyDataSetChanged();

}

}

};

sObserver=newSMSObserver(handler);

this.getContentResolver().registerContentObserver(Uri.parse("content:

//sms"),true,sObserver);

 

 

>2.3. SMSInbox仅用于显示收信箱 故定义SMSDetailsextendsActivity用于详细显示sms信息 

 

-2.3.1.定义布局:

details.xml

 

 

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

orientation="vertical"

android:

layout_width="fill_parent"

android:

layout_height="fill_parent"

>

android:

id="@+id/detailsNumber"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

/>

android:

id="@+id/detailsBody"

android:

layout_width="fill_parent"

android:

layout_height="200dip"

/>

android="

android:

orientation="horizontal"

android:

layout_width="300dip"

android:

layout_height="wrap_content"

>

android:

id="@+id/detailsReply"

android:

layout_width="100dip"

android:

layout_height="wrap_content"

android:

layout_gravity="left"

android:

text="回复"

/>

android:

id="@+id/detailsOK"

android:

layout_width="100dip"

android:

layout_height="wrap_content"

android:

layout_gravity="right"

android:

text="确定"

/>

 

 

 

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

break;

}

}

};

btnReply.setOnClickListener(cl);

btnOK.setOnClickListener(cl);

}

publicvoidsendGoNativeNew(Stringaddress){

//nativesendsmsapp

IntentsendIntent=newIntent(Intent.ACTION_SENDTO,Uri.parse("sms:

//"));

//autofill"address"

sendIntent.putExtra("address",address);

startActivity(sendIntent);

}

publicvoidsendBack(){

Intenti=newIntent();

this.setResult(RESULT_OK,i);

this.finish();

}

}

 

 

 

-2.3.3. 点击SMSInbox某项跳转到SMSDetails

 

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(SMSInbox.this,SMSDetails.class);

intent.putExtras(b);

startActivity(intent);

}

});

 

 

 

-2.3.4.其中 item.xml用于定义子项布局

 

 

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

orientation="vertical"

android:

layout_width="fill_parent"

android:

layout_height="fill_parent"

>

android="

android:

orientation="horizontal"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

>

android:

id="@+id/body"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

/>

android:

id="@+id/num"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

paddingLeft="5dip"

/>

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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 管理学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1