一. 收短信
收短信很简单,接受相应的BroadcastReceiver消息即可。代码如下:
public class MyBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
{
SmsMessage msg = null;
Bundle bundle = intent.getExtras();
if (bundle != null)
{
Object[] pdusObj = (Object[]) bundle.get("pdus");
for (Object p : pdusObj)
{
msg = SmsMessage.createFromPdu((byte[]) p);
Date date = new Date(msg.getTimestampMillis());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String receiveTime = format.format(date);
String senderNumber = msg.getOriginatingAddress();
System.out.println("Send SMS "+senderNumber +" " + receiveTime);
return;
}
}
}
}
}
二.发短信
发短信稍微有些麻烦,发短信没有专门的BroadcastReceiver,使用ContentObserver,唯一的问题是这样的话发短信,删短信等onChange会进很多次,解决办法是在onChange中比较当前得到的短信的时间,发出去的短信的时间应该是最新的。代码如下:
import java.text.SimpleDateFormat;
import java.util.Date;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
//This class is for monitor the sended SMS!
@SuppressLint("SimpleDateFormat")
public class SmsObserver extends ContentObserver
{
private Context context;
String time1 = "", time2 = "";
boolean isNoSMS = false;
private static final String[] SMS_PROJECTION = new String[] { "address", "person", "date", "type", "body", };
public SmsObserver(Context context,Handler handler)
{
super(handler);
// TODO Auto-generated constructor stub
this.context = context;
System.out.println("SmsObserver is running");
}
@Override
public void onChange(boolean selfChange)
{
// TODO Auto-generated method stub
super.onChange(selfChange);
Cursor cursor = context.getContentResolver().query(Uri.parse("content://sms/sent"), SMS_PROJECTION, " type=?", new String[] { "2" }, "date desc");
if (cursor != null)
{
if (!cursor.moveToFirst())
{
isNoSMS = true; //There are no SMS.
}
if (cursor.moveToFirst())
{
SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex("date"))));
String address = cursor.getString(cursor.getColumnIndex("address")).toString();
String time = sfd.format(date);
if (time1.equals(""))
{
time1 = time;
time2 = time;
}
time1 = time;
if (time1.compareTo(time2) > 0)
{
time1 = "";
time2 = "";
System.out.println("time0000000---------" + time);
cursor.close();
return;
}
if (isNoSMS)
{
System.out.println("time1111111---------" + time);
isNoSMS = false;
cursor.close();
return;
}
}
}
}
}
在Activity或Service中注册一下:
getApplicationContext().getContentResolver().registerContentObserver(Uri.parse("content://sms"), true, new SmsObserver(this, new Handler()));
最后不要忘记在Manifest添加相应的权限:
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />