SMS管理:收信箱 发信息 编写新信息

SMS管理:收信箱 发信息 编写新信息

文章分类:移动开发

SMS管理

[功能]

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通知注册方

  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. }  
public class SMSObserver extends ContentObserver {
	public final static int SMS_CHANGE = 0;
	
	Handler handle;
	public SMSObserver(Handler h) {
		super(h);
		// TODO Auto-generated constructor stub
		handle = h;
	}
	
	public void onChange(boolean selfChange) {   
        // TODO Auto-generated method stub   
        super.onChange(selfChange);   
        
        //notify SMSInbox & SMSSent
        handle.sendEmptyMessage(SMS_CHANGE);
    }

}

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

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

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

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

  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" ),  nullnullnullnull );   
  5.                     adapter.notifyDataSetChanged();  
  6.                 }  
  7.               
  8.             }  
  9.         };  
  10.         sObserver = new  SMSObserver(handler);  
  11.         this .getContentResolver().registerContentObserver(Uri.parse( "content://sms" ),  true , sObserver);  
  12.           
handler = new Handler(){
        	public void handleMessage(Message msg){
        		if(msg.what == SMSObserver.SMS_CHANGE){
        			cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null); 
        			adapter.notifyDataSetChanged();
        		}
        	
        	}
        };
        sObserver = new SMSObserver(handler);
        this.getContentResolver().registerContentObserver(Uri.parse("content://sms"), true, sObserver);
        

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

- 2.3.1. 定义布局:details.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. <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>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
	android:id="@+id/detailsNumber"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
<TextView  
	android:id="@+id/detailsBody"
    android:layout_width="fill_parent" 
    android:layout_height="200dip" 
    />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="300dip"
    android:layout_height="wrap_content"
    >
<Button  
	android:id="@+id/detailsReply"
    android:layout_width="100dip" 
    android:layout_height="wrap_content" 
    android:layout_gravity="left"
    android:text="回复"
    />
<Button  
	android:id="@+id/detailsOK"
    android:layout_width="100dip" 
    android:layout_height="wrap_content" 
    android:layout_gravity="right"
    android:text="确定"
    />
</LinearLayout>
</LinearLayout>

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

  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. }  
public class SMSDetails extends Activity {

	TextView textNumber,textBody;
	Button btnOK,btnReply;
	
	OnClickListener cl;
	
	String address,body;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        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);
        
        Intent i = this.getIntent();
        Bundle bundle = i.getExtras();
        
        address = bundle.getString("address");
        body = bundle.getString("body");
        
        textNumber.setText("from:"+address);
        textBody.setText("message body:/n"+body); 

        cl = new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				switch(arg0.getId()){
				case R.id.detailsReply:
					sendGoNativeNew(address);
					break;
					
				case R.id.detailsOK:
					sendBack();
					break;
				}
			}
        	
        };
        
        btnReply.setOnClickListener(cl);
        btnOK.setOnClickListener(cl);
    }
    
    public void sendGoNativeNew(String address){
    	//native send sms app
        Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("sms://"));
        //auto fill "address"
        sendIntent.putExtra("address", address);
        
        startActivity(sendIntent);
    }
    
    public void sendBack(){
    	Intent i = new Intent();
    	this.setResult(RESULT_OK, i);
    	this.finish();
    }
}

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

  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.         });  
lv.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				cursor.moveToPosition(arg2);
				String body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString(); 
				String address = cursor.getString(cursor.getColumnIndexOrThrow("address")).toString();
				
				Bundle b = new Bundle();
				b.putString("body", body);
				b.putString("address", address);
				
				Intent intent = new Intent(SMSInbox.this,SMSDetails.class);
				intent.putExtras(b);
				
				startActivity(intent);
			}
        	
        });

- 2.3.4. 其中 item.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大同小异 故不再细说 仅补上代码

  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" ),  nullnullnullnull );   
  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" ),  nullnullnullnull );   
  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. }  
public class SMSSent extends Activity {
	ListView lv;
	
	Cursor cursor;
	ItemAdapter adapter;
	
	SMSObserver sObserver;
	Handler handler;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        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 = new ItemAdapter(this);
        lv.setAdapter(adapter);
        
        lv.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				cursor.moveToPosition(arg2);
				String body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString(); 
				String address = cursor.getString(cursor.getColumnIndexOrThrow("address")).toString();
				
				Bundle b = new Bundle();
				b.putString("body", body);
				b.putString("address", address);
				
				Intent intent = new Intent(SMSSent.this,SMSDetails.class);
				intent.putExtras(b);
				
				startActivity(intent);
			}
        	
        });
        
        //register SMSObserve
        handler = new Handler(){
        	public void handleMessage(Message msg){
        		if(msg.what == SMSObserver.SMS_CHANGE){
        			cursor = getContentResolver().query(Uri.parse("content://sms/sent"), null, null, null, null); 
        			adapter.notifyDataSetChanged();
        		}
        	
        	}
        };
        sObserver = new SMSObserver(handler);
        this.getContentResolver().registerContentObserver(Uri.parse("content://sms"), true, sObserver);
        
    }
    
    
    public class ItemAdapter extends BaseAdapter {
    	Activity activity;
    	
    	public ItemAdapter(Activity a){
    		activity = a;
    	}
    	
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return cursor.getCount();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return cursor.getString(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			// TODO Auto-generated method stub
			return composeItem(arg0);
		}
		
		private View composeItem(int position){
			cursor.moveToPosition(position);
			String body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString(); 
			String number = cursor.getString(cursor.getColumnIndexOrThrow("address")).toString();
			
			LinearLayout item = (LinearLayout)activity.getLayoutInflater().inflate(R.layout.item, null);
			
			LinearLayout l1 = (LinearLayout)item.getChildAt(0);
			
			TextView tbody = (TextView)item.getChildAt(1);
			if(tbody != null){
				tbody.setText(body);
			}
			
			TextView tnum = (TextView)l1.getChildAt(1);
			if(tnum != null){
				tnum.setText(number);
			}
			
			ImageView image = (ImageView)l1.getChildAt(0);
			image.setImageResource(R.drawable.message);
			
			return item;
		}
    	
    }
}

4. emulator 运行截图

 > 4.1. SMSInbox:

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

- 4.1.2. 短信发送记录为:

  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  
Android Console: type 'help' for a list of commands
OK
sms send 12 ds
OK
sms send 23 hi to 23
OK
sms send 34 i am griddinshi
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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值