网站带后台模板,设计常去的网站,修改网站域名,网站建设询价原标题#xff1a;Android实现短信自动回复#xff0c;挂电话简单功能#xff0c;配置一下ITelephoney,ITelephony这个接口不能直接用的。需要先在src下新建包com.android.internal.telephony#xff0c;在其中新建一个File#xff0c;后缀为aidl(它是一种android内部进程…原标题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; //用来存储来电号码OverridepublicvoidonCreate(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() {OverridepublicvoidonClick(View v){Editor editor sp.edit();editor.putString( sms, et.getText().toString());editor.commit(); //这里是默认自动保存用户编辑过的回复短信内容的,}});}classMyPhoneStateListenerextendsPhoneStateListener{OverridepublicvoidonCallStateChanged(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;}};};}权限设置返回搜狐查看更多责任编辑