android短信机制

android删除短信整个会话

1. getContentResolver().delete( 2. Uri.parse("content://sms/conversations/" + threadid, 3. "_id = " + idelete, null);

[功能]

1. 收信箱:显示所有收到的信息 且实时显示 即:当有新信息收到 能自动刷新显示

2. 发信箱:显示所有已发信息 同上

3. 编写新信息: 鉴于一些问题 打算不自行定义 而只通过Intent调用系统的

[原理]

1. 通过目标Uri显示收信箱 发信箱 目标Uri:content://sms/inbox content://sms/sent

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

3. 注意:ContentObserve不能监听: content://sms/inbox & content://sms/sent 而只能监听content://sms

[代码 步骤]

1. 定义SMSObserver 用于监听目标 并通过Handle通知注册方

Java 代码
  1. public class SMSObserver extends ContentObserver{
  2. public final static int SMS_CHANGE= 0 ;
  3. Handlerhandle;
  4. public SMSObserver(Handlerh){
  5. super (h);
  6. //TODOAuto-generatedconstructorstub
  7. handle=h;
  8. }
  9. public void onChange( boolean selfChange){
  10. //TODOAuto-generatedmethodstub
  11. super .onChange(selfChange);
  12. //notifySMSInbox&SMSSent
  13. handle.sendEmptyMessage(SMS_CHANGE);
  14. }
  15. }

2. 定义注册方:SMSInbox 鉴于SMSSent与其原理类似 故打算以SMSInbox为例

> 2.1. 显示当前所有收信箱 并与ListView适配

Java代码
  1. lv=(ListView)findViewById(R.id.list);
  2. cursor=getContentResolver().query(Uri.parse("content://sms/inbox" ), null , null , null , null );
  3. adapter=new ItemAdapter( this );
  4. lv.setAdapter(adapter);

> 2.2. 定义Handle 用于接受变动 并注册与ContentObserve 当接到通知后 查询目标Uri 并刷新显示

Java代码
  1. handler= new Handler(){
  2. public void handleMessage(Messagemsg){
  3. if (msg.what==SMSObserver.SMS_CHANGE){
  4. cursor=getContentResolver().query(Uri.parse("content://sms/inbox" ), null , null , null , null );
  5. adapter.notifyDataSetChanged();
  6. }
  7. }
  8. };
  9. sObserver=new SMSObserver(handler);
  10. this .getContentResolver().registerContentObserver(Uri.parse( "content://sms" ), true ,sObserver);

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

- 2.3.1. 定义布局:details.xml

Java代码
  1. <?xmlversion= "1.0" encoding= "utf-8" ?>
  2. <LinearLayoutxmlns:Android ="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:id="@+id/detailsNumber"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. />
  12. <TextView
  13. android:id="@+id/detailsBody"
  14. android:layout_width="fill_parent"
  15. android:layout_height="200dip"
  16. />
  17. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  18. android:orientation="horizontal"
  19. android:layout_width="300dip"
  20. android:layout_height="wrap_content"
  21. >
  22. <Button
  23. android:id="@+id/detailsReply"
  24. android:layout_width="100dip"
  25. android:layout_height="wrap_content"
  26. android:layout_gravity="left"
  27. android:text="回复"
  28. />
  29. <Button
  30. android:id="@+id/detailsOK"
  31. android:layout_width="100dip"
  32. android:layout_height="wrap_content"
  33. android:layout_gravity="right"
  34. android:text="确定"
  35. />
  36. </LinearLayout>
  37. </LinearLayout>

- 2.3.2. 其中2个TextView 分别显示信息地址和正文 2个Button 一个用于关闭当前窗口 一个用于短信回复 且自动填充 收信人地址

Java代码
  1. public class SMSDetails extends Activity{
  2. TextViewtextNumber,textBody;
  3. ButtonbtnOK,btnReply;
  4. OnClickListenercl;
  5. Stringaddress,body;
  6. /**Calledwhentheactivityisfirstcreated.*/
  7. @Override
  8. public void onCreate(BundlesavedInstanceState){
  9. super .onCreate(savedInstanceState);
  10. setContentView(R.layout.details);
  11. textNumber=(TextView)findViewById(R.id.detailsNumber);
  12. textBody=(TextView)findViewById(R.id.detailsBody);
  13. btnReply=(Button)findViewById(R.id.detailsReply);
  14. btnOK=(Button)findViewById(R.id.detailsOK);
  15. Intenti=this .getIntent();
  16. Bundlebundle=i.getExtras();
  17. address=bundle.getString("address" );
  18. body=bundle.getString("body" );
  19. textNumber.setText("from:" +address);
  20. textBody.setText("messagebody:\n" +body);
  21. cl=new OnClickListener(){
  22. @Override
  23. public void onClick(Viewarg0){
  24. //TODOAuto-generatedmethodstub
  25. switch (arg0.getId()){
  26. case R.id.detailsReply:
  27. sendGoNativeNew(address);
  28. break ;
  29. case R.id.detailsOK:
  30. sendBack();
  31. break ;
  32. }
  33. }
  34. };
  35. btnReply.setOnClickListener(cl);
  36. btnOK.setOnClickListener(cl);
  37. }
  38. public void sendGoNativeNew(Stringaddress){
  39. //nativesendsmsapp
  40. IntentsendIntent=new Intent(Intent.ACTION_SENDTO,Uri.parse( "sms://" ));
  41. //autofill"address"
  42. sendIntent.putExtra("address" ,address);
  43. startActivity(sendIntent);
  44. }
  45. public void sendBack(){
  46. Intenti=new Intent();
  47. this .setResult(RESULT_OK,i);
  48. this .finish();
  49. }
  50. }

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

Java代码
  1. lv.setOnItemClickListener( new OnItemClickListener(){
  2. @Override
  3. public void onItemClick(AdapterView<?>arg0,Viewarg1, int arg2,
  4. long arg3){
  5. //TODOAuto-generatedmethodstub
  6. cursor.moveToPosition(arg2);
  7. Stringbody=cursor.getString(cursor.getColumnIndexOrThrow("body" )).toString();
  8. Stringaddress=cursor.getString(cursor.getColumnIndexOrThrow("address" )).toString();
  9. Bundleb=new Bundle();
  10. b.putString("body" ,body);
  11. b.putString("address" ,address);
  12. Intentintent=new Intent(SMSInbox. this ,SMSDetails. class );
  13. intent.putExtras(b);
  14. startActivity(intent);
  15. }
  16. });

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

Xml代码
  1. <? xml version = "1.0" encoding = "utf-8" ?>
  2. < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
  3. android:orientation = "vertical"
  4. android:layout_width = "fill_parent"
  5. android:layout_height = "fill_parent"
  6. >
  7. < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
  8. android:orientation = "horizontal"
  9. android:layout_width = "wrap_content"
  10. android:layout_height = "wrap_content"
  11. >
  12. < ImageView
  13. android:id = "@+id/body"
  14. android:layout_width = "wrap_content"
  15. android:layout_height = "wrap_content"
  16. />
  17. < TextView
  18. android:id = "@+id/num"
  19. android:layout_width = "wrap_content"
  20. android:layout_height =
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值