一、短信接收广播:
在AndroidManifest.xml文件中的
<
application
>
节点里对接收到短信的广播Intent进行订阅:
< receiver android:name =".IncomingSMSReceiver" >
< intent-filter >< action android:name ="android.provider.Telephony.SMS_RECEIVED" /></ intent-filter ></ receiver >
在AndroidManifest.xml文件中添加以下权限:
< uses-permission android:name ="android.permission.RECEIVE_SMS" /> <!-- 接收短信权限 -->
< uses-permission android:name ="android.permission.SEND_SMS" /> <!-- 发送短信权限 -->
< receiver android:name =".IncomingSMSReceiver" >
< intent-filter >< action android:name ="android.provider.Telephony.SMS_RECEIVED" /></ intent-filter ></ receiver >
在AndroidManifest.xml文件中添加以下权限:
< uses-permission android:name ="android.permission.RECEIVE_SMS" /> <!-- 接收短信权限 -->
< uses-permission android:name ="android.permission.SEND_SMS" /> <!-- 发送短信权限 -->
public
class
SmsBroadcastReceiver
extends
BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Object[] pduses = (Object[])intent.getExtras().get( " pdus " );
for (Object pdus: pduses){
byte [] pdusSms = ( byte [])pdus;
SmsMessage smsMessage = SmsMessage.createFromPdu(pdusSms);
String mobile = smsMessage.getOriginatingAddress(); // 获得发短信手机
String content = smsMessage.getMessageBody(); // 获得短信内容
Date date = new Date(smsMessage.getTimestampMillis()); // 获得短信发送时间
SimpleDateFormat simpleDateFormat = new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
String sendDate = simpleDateFormat.format(date);
}
}
}
@Override
public void onReceive(Context context, Intent intent) {
Object[] pduses = (Object[])intent.getExtras().get( " pdus " );
for (Object pdus: pduses){
byte [] pdusSms = ( byte [])pdus;
SmsMessage smsMessage = SmsMessage.createFromPdu(pdusSms);
String mobile = smsMessage.getOriginatingAddress(); // 获得发短信手机
String content = smsMessage.getMessageBody(); // 获得短信内容
Date date = new Date(smsMessage.getTimestampMillis()); // 获得短信发送时间
SimpleDateFormat simpleDateFormat = new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
String sendDate = simpleDateFormat.format(date);
}
}
}
二、电话通话接收者: