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通知注册方
- public class SMSObserver extends ContentObserver{
- public final static int SMS_CHANGE= 0 ;
- Handlerhandle;
- public SMSObserver(Handlerh){
- super (h);
- //TODOAuto-generatedconstructorstub
- handle=h;
- }
- public void onChange( boolean selfChange){
- //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=new ItemAdapter( this );
- lv.setAdapter(adapter);
> 2.2. 定义Handle 用于接受变动 并注册与ContentObserve 当接到通知后 查询目标Uri 并刷新显示
- handler= new Handler(){
- public void handleMessage(Messagemsg){
- 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
- <?xmlversion= "1.0" encoding= "utf-8" ?>
- <LinearLayoutxmlns: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"
- />
- <LinearLayoutxmlns: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 一个用于关闭当前窗口 一个用于短信回复 且自动填充 收信人地址
- public class SMSDetails extends Activity{
- TextViewtextNumber,textBody;
- ButtonbtnOK,btnReply;
- OnClickListenercl;
- Stringaddress,body;
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- public void onCreate(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=new OnClickListener(){
- @Override
- public void onClick(Viewarg0){
- //TODOAuto-generatedmethodstub
- 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(Stringaddress){
- //nativesendsmsapp
- IntentsendIntent=new Intent(Intent.ACTION_SENDTO,Uri.parse( "sms://" ));
- //autofill"address"
- sendIntent.putExtra("address" ,address);
- startActivity(sendIntent);
- }
- public void sendBack(){
- Intenti=new Intent();
- this .setResult(RESULT_OK,i);
- this .finish();
- }
- }
- 2.3.3. 点击SMSInbox 某项 跳转到SMSDetails
- lv.setOnItemClickListener( new OnItemClickListener(){
- @Override
- public void onItemClick(AdapterView<?>arg0,Viewarg1, int arg2,
- long arg3){
- //TODOAuto-generatedmethodstub
- cursor.moveToPosition(arg2);
- Stringbody=cursor.getString(cursor.getColumnIndexOrThrow("body" )).toString();
- Stringaddress=cursor.getString(cursor.getColumnIndexOrThrow("address" )).toString();
- Bundleb=new Bundle();
- b.putString("body" ,body);
- b.putString("address" ,address);
- Intentintent=new Intent(SMSInbox. this ,SMSDetails. class );
- intent.putExtras(b);
- startActivity(intent);
- }
- });
- 2.3.4. 其中item.xml 用于定义子项布局
- <? 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"
- >
- < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
- android:orientation = "horizontal"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- >
- < ImageView
- android:id = "@+id/body"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- />
- < TextView
- android:id = "@+id/num"
- android:layout_width = "wrap_content"
- android:layout_height =