原标题:Android实现短信自动回复,挂电话
简单功能,配置一下ITelephoney,ITelephony这个接口不能直接用的。
需要先在src下新建包com.android.internal.telephony,在其中新建一个File,后缀为aidl(它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口),内容如下
packagecom.android.internal.telephony;
interfaceITelephony{
booleanendCall();
voidanswerRingingCall();
}
接下来就是一下MainActivity,垃圾代码如下。
packagecom.example.autoreply;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.app.Activity;
importjava.lang.reflect.Method;
importjava.util.HashMap;
importjava.util.List;
importcom.android.internal.telephony.ITelephony;
importandroid.content.Context;
importandroid.content.SharedPreferences;
importandroid.content.SharedPreferences.Editor;
importandroid.telephony.PhoneStateListener;
importandroid.telephony.SmsManager;
importandroid.telephony.TelephonyManager;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.view.Window;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.TextView;
publicclassMainActivityextendsActivity{
Button btn; //退出按钮
EditText et; //回复短信的内容编辑框
TextView tv; //拦截数量通知的显示
TelephonyManager tpm;
SharedPreferences sp;
intcount = 0; //来电总数
intpeo = 0; //来电的号码个数,跟来电总数有区别,这个不包括重复来电,
String num; //存储来电号码
HashMap numMap; //用来存储来电号码
@Override
publicvoidonCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); // 注意顺序
setContentView(R.layout.activity_main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, // 注意顺序
R.layout.title);
tv = (TextView) findViewById(R.id.textView1);
et = (EditText) findViewById(R.id.editText1);
btn = (Button) findViewById(R.id.button1);
sp = this.getSharedPreferences( "SP", MODE_PRIVATE);
numMap = newHashMap();
if(sp.getString( "sms", null) != null){
et.setText(sp.getString( "sms", "我现在正在上课,一会儿下课了联系你"));
}
tpm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); //获取电话通讯服务
tpm.listen( newMyPhoneStateListener(),PhoneStateListener.LISTEN_CALL_STATE); //给电话服务增加状态监听器,监听来电,通话,挂断等状态
btn.setOnClickListener( newOnClickListener() {
@Override
publicvoidonClick(View v){
Editor editor = sp.edit();
editor.putString( "sms", et.getText().toString());
editor.commit(); //这里是默认自动保存用户编辑过的回复短信内容的,
}
});
}
classMyPhoneStateListenerextendsPhoneStateListener{
@Override
publicvoidonCallStateChanged(intstate, String incomingNumber){
num = incomingNumber;
switch(state) {
caseTelephonyManager.CALL_STATE_IDLE: //空闲
break;
caseTelephonyManager.CALL_STATE_RINGING: //来电
endCall(); //自动挂断
if(!numMap.containsKey(num)){ //如果之前没有来电,把这个号码加入已经来电过的列表
sendMes();
numMap.put(num, null);
peo ++;
updateUi(); //更新来电数目
}
break;
caseTelephonyManager.CALL_STATE_OFFHOOK: //摘机(正在通话中)
break;
}
}
privatevoidupdateUi(){
if(count > 0){
tv.setVisibility(View.VISIBLE);
}
handler.sendEmptyMessage( 0);
// tv.setText("已拒接" + count + "个来电,共" + peo +"个人联系过您");
}
privatevoidendCall()
{
Class c = TelephonyManager.class;
try
{
Method getITelephonyMethod = c.getDeclaredMethod( "getITelephony", (Class[]) null);
getITelephonyMethod.setAccessible( true);
ITelephony iTelephony = null;
iTelephony = (ITelephony) getITelephonyMethod.invoke(tpm, (Object[]) null);
iTelephony.endCall();
count ++;
updateUi();
}
catch(Exception e)
{
}
}
privatevoidsendMes(){
//直接调用短信接口发短信
SmsManager smsManager = SmsManager.getDefault();
List divideContents = smsManager.divideMessage(et.getText().toString());
for(String text : divideContents) {
smsManager.sendTextMessage(num, null, text, null, null);
}
}
}
publicHandler handler = newHandler()
{
publicvoidhandleMessage(android.os.Message msg)
{
switch(msg.what)
{
// 接收的内容
case0:
tv.setText( "已拒接"+ count + "个来电,共"+ peo + "个人联系过您");
break;
default:
break;
}
};
};
}
权限设置:
返回搜狐,查看更多
责任编辑: