短信操作

一、监听短信的接收

1.注册广播接收器 

<receiver android:name="SMSReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

 

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver {

public static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

@Override
public void onReceive(Context context, Intent intent) {
if (SMS_RECEIVED.equals(intent.getAction())) {
Bundle bundle
= intent.getExtras();
if (bundle != null) {
Object[] pdus
= (Object[]) bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
String msg
= "";
for (int i = 0; i < pdus.length; i++) {
messages[i]
= SmsMessage.createFromPdu((byte[]) pdus[i]);
msg
+= messages[i].getMessageBody();
}
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
}
}
}

}

2.添加权限 

<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>

 

 二、读取收件箱中的短信

1.从数据库中读取,URI为"content://sms/inbox" 

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;

public class MySMSManager extends Activity {

private TextView textview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textview
= (TextView) findViewById(R.id.textview);
readShortMessage();
}

public void readShortMessage() {
ContentResolver cr
= getContentResolver();
Cursor cursor
= cr.query(Uri.parse("content://sms/inbox"), null, null, null, null);
String msg
= "";
while(cursor.moveToNext()) {
int phoneColumn = cursor.getColumnIndex("address");
int smsColumn = cursor.getColumnIndex("body");
msg
+= cursor.getString(phoneColumn) + ":" + cursor.getString(smsColumn) + "\n";
}
textview.setText(msg);
}
}

2.添加权限 

<uses-permission android:name="android.permission.READ_SMS"></uses-permission>

 

 附:其它短信的URI和数据库表字段 

String SMS_URI_ALL = "content://sms/";
String SMS_URI_INBOX
= "content://sms/inbox";
String SMS_URI_SENT
= "content://sms/sent";
String SMS_URI_DRAFT
= "content://sms/draft";
String SMS_URI_OUTBOX
= "content://sms/outbox";
String SMS_URI_FAILED
= "content://sms/failed";
String SMS_URI_QUEUED
= "content://sms/queued";

 

/*
_id => 短消息序号 如100
thread_id => 对话的序号 如100
address => 发件人地址,手机号.如+8613811810000
person => 发件人,返回一个数字就是联系人列表里的序号,陌生人为null
date => 日期 long型。如1256539465022
protocol => 协议 0 SMS_RPOTO, 1 MMS_PROTO
read => 是否阅读 0未读, 1已读
status => 状态 -1接收,0 complete, 64 pending, 128 failed
type => 类型 1是接收到的,2是已发出
body => 短消息内容
service_center => 短信服务中心号码编号。如+8613800755500
*/

 

三、打开发送短信界面

 

Uri uri = Uri.parse("smsto:13800138000");
Intent it
= new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra(
"sms_body", "The SMS text");
startActivity(it);

 

 

四、发送短信

1.通过SmsManager的sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent)方法发送


String msg ="hello";
String number
= "1234565678";
SmsManager sms
= SmsManager.getDefault();
PendingIntent pi
= PendingIntent.getBroadcast(this,0,new Intent(),0);
sms.sendTextMessage(number,
null,msg,pi,null);

 

2.添加权限

<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>

 

五、直接向数据库写短信

 

ContentResolver cr = getContentResolver();
ContentValues cv
= new ContentValues();
cv.put(
"address", "13800138000");
cv.put(
"body", "hello!");
cr.insert(Uri.parse(
"content://sms/inbox"), cv);

 

权限:

<uses-permission android:name="android.permission.WRITE_SMS"></uses-permission>

 

 六、监听短信数据库变化

使用ContentObserver ,观察"content://sms"的变化,调用重写的onChange方法,可以监听到短信记录的变化,这样可以监听发短信,同样也是可以监听收短信的。

import android.database.ContentObserver;
import android.os.Handler;
import android.util.Log;

public class SMSObserver extends ContentObserver {

public SMSObserver(Handler handler) {
super(handler);
}

@Override
public void onChange(boolean selfChange) {
Log.i(
"sms", "sms");
}

}

 

然后在Acitivty或Service里注册这个观察者

getContentResolver().registerContentObserver(Uri.parse("content://sms"),
true, new SMSObserver(new Handler()));

 

posted @ 2010-12-26 16:36  penink  阅读(1548)  评论(0编辑  收藏  举报