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