电话短信

android 电话
/*
* incomingNumber参数只有在来电状态下才会有值,所有在来电后记得将该值保存,以方便在其他地方使用
* CALL_STATE_RINGING: 来电状态
* CALL_STATE_OFFHOOK: 摘机状态,即接听状态
* CALL_STATE_IDLE:空闲状态
*/

public class MyReceiver extends BroadcastReceiver{

private static final String TAG = "MyReceiver";

// private static MyPhoneStateListener phoneListener = new MyPhoneStateListener();

private static boolean incomingFlag = false;

private static String incoming_number = null;
private SmsManager sms;
private List<String> messages;

@Override
public void onReceive(Context context, Intent intent) {
//如果是拨打电话
if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
incomingFlag = false;
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i(TAG, "call OUT:"+phoneNumber);
//获取SmsManager

sms=SmsManager.getDefault();

//如果内容大于70字,则拆分为多条

messages=sms.divideMessage(incoming_number);

//逐条发送短信

for(String message:messages)

{

sms.sendTextMessage(incoming_number, null, message, null, null);

}
}else{
//如果是来电
TelephonyManager tm =
(TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);

switch (tm.getCallState()) {
case TelephonyManager.CALL_STATE_RINGING:
incomingFlag = true;//标识当前是来电
incoming_number = intent.getStringExtra("incoming_number");
Log.i(TAG, "RINGING :"+ incoming_number);
//获取SmsManager

sms=SmsManager.getDefault();

//如果内容大于70字,则拆分为多条

messages=sms.divideMessage(incoming_number);

//逐条发送短信

for(String text:messages)

{

sms.sendTextMessage(incoming_number, null, text, null, null);

}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if(incomingFlag){
Log.i(TAG, "incoming ACCEPT :"+ incoming_number);
}
//获取SmsManager

sms=SmsManager.getDefault();

//如果内容大于70字,则拆分为多条

messages=sms.divideMessage(incoming_number);

//逐条发送短信

for(String text:messages)

{

sms.sendTextMessage(incoming_number, null, text, null, null);

}
break;

case TelephonyManager.CALL_STATE_IDLE:
if(incomingFlag){
Log.i(TAG, "incoming IDLE");
}
//获取SmsManager

sms=SmsManager.getDefault();

//如果内容大于70字,则拆分为多条

messages=sms.divideMessage(incoming_number);

//逐条发送短信

for(String text:messages)

{

sms.sendTextMessage(incoming_number, null, text, null, null);

}
break;
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值