android自动回复退订,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; //用來存儲來電號碼
@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;
}
};
};
}
權限設置:
返回搜狐,查看更多
責任編輯:
總結
以上是生活随笔為你收集整理的android自动回复退订,Android实现短信自动回复,挂电话的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux的实际操作:文件目录类实用指令
- 下一篇: JAVA进阶开发之(内部类概述)