android短信机制

android删除短信整个会话

 

 

 

 

[功能]

 

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.       
  4.     Handler handle;  
  5.     public  SMSObserver(Handler h) {  
  6.         super (h);  
  7.         // TODO Auto-generated constructor stub   
  8.         handle = h;  
  9.     }  
  10.       
  11.     public   void  onChange( boolean  selfChange) {     
  12.         // TODO Auto-generated method stub      
  13.         super .onChange(selfChange);     
  14.           
  15.         //notify SMSInbox & SMSSent   
  16.         handle.sendEmptyMessage(SMS_CHANGE);  
  17.     }  
  18.   
  19. }  

 

 

 

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

 

 

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

 

 

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

 

 

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

 

 

Java代码 
  1. handler =  new  Handler(){  
  2.             public   void  handleMessage(Message msg){  
  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.         };  
  10.         sObserver = new  SMSObserver(handler);  
  11.         this .getContentResolver().registerContentObserver(Uri.parse( "content://sms" ),  true , sObserver);  
  12.           

 

 

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

 

- 2.3.1. 定义布局:details.xml

 

 

Java代码 
  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. <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. <LinearLayout xmlns: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.   
  3.     TextView textNumber,textBody;  
  4.     Button btnOK,btnReply;  
  5.       
  6.     OnClickListener cl;  
  7.       
  8.     String address,body;  
  9.     /** Called when the activity is first created. */   
  10.     @Override   
  11.     public   void  onCreate(Bundle savedInstanceState) {  
  12.         super .onCreate(savedInstanceState);  
  13.         setContentView(R.layout.details);  
  14.           
  15.         textNumber = (TextView)findViewById(R.id.detailsNumber);  
  16.         textBody = (TextView)findViewById(R.id.detailsBody);  
  17.           
  18.         btnReply = (Button)findViewById(R.id.detailsReply);  
  19.         btnOK = (Button)findViewById(R.id.detailsOK);  
  20.           
  21.         Intent i = this .getIntent();  
  22.         Bundle bundle = i.getExtras();  
  23.           
  24.         address = bundle.getString("address" );  
  25.         body = bundle.getString("body" );  
  26.           
  27.         textNumber.setText("from:" +address);  
  28.         textBody.setText("message body:/n" +body);   
  29.   
  30.         cl = new  OnClickListener(){  
  31.   
  32.             @Override   
  33.             public   void  onClick(View arg0) {  
  34.                 // TODO Auto-generated method stub   
  35.                 switch (arg0.getId()){  
  36.                 case  R.id.detailsReply:  
  37.                     sendGoNativeNew(address);  
  38.                     break ;  
  39.                       
  40.                 case  R.id.detailsOK:  
  41.                     sendBack();  
  42.                     break ;  
  43.                 }  
  44.             }  
  45.               
  46.         };  
  47.           
  48.         btnReply.setOnClickListener(cl);  
  49.         btnOK.setOnClickListener(cl);  
  50.     }  
  51.       
  52.     public   void  sendGoNativeNew(String address){  
  53.         //native send sms app   
  54.         Intent sendIntent = new  Intent(Intent.ACTION_SENDTO, Uri.parse( "sms://" ));  
  55.         //auto fill "address"   
  56.         sendIntent.putExtra("address" , address);  
  57.           
  58.         startActivity(sendIntent);  
  59.     }  
  60.       
  61.     public   void  sendBack(){  
  62.         Intent i = new  Intent();  
  63.         this .setResult(RESULT_OK, i);  
  64.         this .finish();  
  65.     }  
  66. }  

 

 

 

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

 

Java代码 
  1. lv.setOnItemClickListener( new  OnItemClickListener(){  
  2.             @Override   
  3.             public   void  onItemClick(AdapterView<?> arg0, View arg1,  int  arg2,  
  4.                     long  arg3) {  
  5.                 // TODO Auto-generated method stub   
  6.                 cursor.moveToPosition(arg2);  
  7.                 String body = cursor.getString(cursor.getColumnIndexOrThrow("body" )).toString();   
  8.                 String address = cursor.getString(cursor.getColumnIndexOrThrow("address" )).toString();  
  9.                   
  10.                 Bundle b = new  Bundle();  
  11.                 b.putString("body" , body);  
  12.                 b.putString("address" , address);  
  13.                   
  14.                 Intent intent = new  Intent(SMSInbox. this ,SMSDetails. class );  
  15.                 intent.putExtras(b);  
  16.                   
  17.                 startActivity(intent);  
  18.             }  
  19.               
  20.         });  

 

 

 

- 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 = "wrap_content"    
  21.     android:paddingLeft = "5dip"   
  22.     />   
  23. </ LinearLayout >   
  24. < TextView     
  25.     android:id = "@+id/body"   
  26.     android:layout_width = "fill_parent"    
  27.     android:layout_height = "wrap_content"    
  28.     android:paddingLeft = "10dip"   
  29.     />   
  30. </ LinearLayout >   

 

 

 

3. 鉴于SMSSent与SMSInbox大同小异 故不再细说 仅补上代码

 

Java代码 
  1. public   class  SMSSent  extends  Activity {  
  2.     ListView lv;  
  3.       
  4.     Cursor cursor;  
  5.     ItemAdapter adapter;  
  6.       
  7.     SMSObserver sObserver;  
  8.     Handler handler;  
  9.       
  10.     /** Called when the activity is first created. */   
  11.     @Override   
  12.     public   void  onCreate(Bundle savedInstanceState) {  
  13.         super .onCreate(savedInstanceState);  
  14.         setContentView(R.layout.list);  
  15.         setTitle(R.string.sent);  
  16.           
  17.         lv = (ListView)findViewById(R.id.list);  
  18.           
  19.         cursor = getContentResolver().query(Uri.parse("content://sms/sent" ),  null null null null );   
  20.           
  21.         adapter = new  ItemAdapter( this );  
  22.         lv.setAdapter(adapter);  
  23.           
  24.         lv.setOnItemClickListener(new  OnItemClickListener(){  
  25.             @Override   
  26.             public   void  onItemClick(AdapterView<?> arg0, View arg1,  int  arg2,  
  27.                     long  arg3) {  
  28.                 // TODO Auto-generated method stub   
  29.                 cursor.moveToPosition(arg2);  
  30.                 String body = cursor.getString(cursor.getColumnIndexOrThrow("body" )).toString();   
  31.                 String address = cursor.getString(cursor.getColumnIndexOrThrow("address" )).toString();  
  32.                   
  33.                 Bundle b = new  Bundle();  
  34.                 b.putString("body" , body);  
  35.                 b.putString("address" , address);  
  36.                   
  37.                 Intent intent = new  Intent(SMSSent. this ,SMSDetails. class );  
  38.                 intent.putExtras(b);  
  39.                   
  40.                 startActivity(intent);  
  41.             }  
  42.               
  43.         });  
  44.           
  45.         //register SMSObserve   
  46.         handler = new  Handler(){  
  47.             public   void  handleMessage(Message msg){  
  48.                 if (msg.what == SMSObserver.SMS_CHANGE){  
  49.                     cursor = getContentResolver().query(Uri.parse("content://sms/sent" ),  null null null null );   
  50.                     adapter.notifyDataSetChanged();  
  51.                 }  
  52.               
  53.             }  
  54.         };  
  55.         sObserver = new  SMSObserver(handler);  
  56.         this .getContentResolver().registerContentObserver(Uri.parse( "content://sms" ),  true , sObserver);  
  57.           
  58.     }  
  59.       
  60.       
  61.     public   class  ItemAdapter  extends  BaseAdapter {  
  62.         Activity activity;  
  63.           
  64.         public  ItemAdapter(Activity a){  
  65.             activity = a;  
  66.         }  
  67.           
  68.         @Override   
  69.         public   int  getCount() {  
  70.             // TODO Auto-generated method stub   
  71.             return  cursor.getCount();  
  72.         }  
  73.   
  74.         @Override   
  75.         public  Object getItem( int  arg0) {  
  76.             // TODO Auto-generated method stub   
  77.             return  cursor.getString(arg0);  
  78.         }  
  79.   
  80.         @Override   
  81.         public   long  getItemId( int  arg0) {  
  82.             // TODO Auto-generated method stub   
  83.             return  arg0;  
  84.         }  
  85.   
  86.         @Override   
  87.         public  View getView( int  arg0, View arg1, ViewGroup arg2) {  
  88.             // TODO Auto-generated method stub   
  89.             return  composeItem(arg0);  
  90.         }  
  91.           
  92.         private  View composeItem( int  position){  
  93.             cursor.moveToPosition(position);  
  94.             String body = cursor.getString(cursor.getColumnIndexOrThrow("body" )).toString();   
  95.             String number = cursor.getString(cursor.getColumnIndexOrThrow("address" )).toString();  
  96.               
  97.             LinearLayout item = (LinearLayout)activity.getLayoutInflater().inflate(R.layout.item, null );  
  98.               
  99.             LinearLayout l1 = (LinearLayout)item.getChildAt(0 );  
  100.               
  101.             TextView tbody = (TextView)item.getChildAt(1 );  
  102.             if (tbody !=  null ){  
  103.                 tbody.setText(body);  
  104.             }  
  105.               
  106.             TextView tnum = (TextView)l1.getChildAt(1 );  
  107.             if (tnum !=  null ){  
  108.                 tnum.setText(number);  
  109.             }  
  110.               
  111.             ImageView image = (ImageView)l1.getChildAt(0 );  
  112.             image.setImageResource(R.drawable.message);  
  113.               
  114.             return  item;  
  115.         }  
  116.           
  117.     }  
  118. }  

 

 

 

4. emulator 运行截图

 

 > 4.1. SMSInbox:

 

- 4.1.1. 通过telnet localhost 5554 登录emulator  通过 sms send 123 hello to 123 模拟发送短信

 

- 4.1.2. 短信发送记录为:

 

Java代码 
  1. Android Console: type  'help'   for  a list of commands  
  2. OK  
  3. sms send 12  ds  
  4. OK  
  5. sms send 23  hi to  23   
  6. OK  
  7. sms send 34  i am griddinshi  
  8. OK  

 

- 4.1.3. SMSInbox:

 

 

 

> 4.2. SMSSent

 

- 4.2.1. 已发短信记录:

 

 

- 4.2.2. SMSSent:

 

 

 

 

 

5. 未解决问题:

 

> 5.1. ContentObserver 只能监听content://sms  而不支持content://sms/inbox content://sms/sent  个人猜测是因为:android 在写sms数据库 insert(...) 没有通过ContentResolver通知content://sms/inbox content://sms/sent 所致 即:没有以下代码:

 

getContext().getContentResolver().notifyChange(noteUri, null);

 

 

6. 如果写的有问题的 欢迎讨论  否则 请回帖支持一些

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值