Android实现短信自动回复,挂电话

 简单功能,配置一下ITelephoney,ITelephony这个接口不能直接用的。

需要先在src新建包com.android.internal.telephony,在其中新建一个File,后缀为aidl(它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口),内容如下

package com.android.internal.telephony;  
interface ITelephony{  
boolean endCall();  
void answerRingingCall();  
} 
接下来就是一下MainActivity,垃圾代码如下。
package com.example.autoreply;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import java.lang.reflect.Method;  
import java.util.HashMap;  
import java.util.List;  
import com.android.internal.telephony.ITelephony;
import android.content.Context;  
import android.content.SharedPreferences;  
import android.content.SharedPreferences.Editor;    
import android.telephony.PhoneStateListener;  
import android.telephony.SmsManager;  
import android.telephony.TelephonyManager;   
import android.view.View;  
import android.view.View.OnClickListener;  
import android.view.Window;
import android.widget.Button;  
import android.widget.EditText;  
import android.widget.TextView;  
     
public class MainActivity extends Activity {  
                
    Button btn;//退出按钮  
    EditText et;//回复短信的内容编辑框  
    TextView tv;//拦截数量通知的显示  
    
    TelephonyManager tpm;
    SharedPreferences sp; 
    
    int count = 0;//来电总数  
    int peo = 0;//来电的号码个数,跟来电总数有区别,这个不包括重复来电,  
    String num;//存储来电号码
    
    HashMap<String, String> numMap;//用来存储来电号码  
    
    @Override  
    public void onCreate(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 = new HashMap<String, String>();  
        if(sp.getString("sms", null) != null){  
            et.setText(sp.getString("sms", "我现在正在上课,一会儿下课了联系你"));  
        }  
        tpm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);//获取电话通讯服务  
        tpm.listen(new MyPhoneStateListener(),PhoneStateListener.LISTEN_CALL_STATE);//给电话服务增加状态监听器,监听来电,通话,挂断等状态  
      
        btn.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                Editor editor = sp.edit();  
                editor.putString("sms", et.getText().toString());  
                editor.commit();//这里是默认自动保存用户编辑过的回复短信内容的,    
            }  
        });  
    }  
    class MyPhoneStateListener extends PhoneStateListener {  
        @Override  
        public void onCallStateChanged(int state, String incomingNumber) {  
            num = incomingNumber;  
            switch(state) {  
            case TelephonyManager.CALL_STATE_IDLE: //空闲  
                break;  
            case TelephonyManager.CALL_STATE_RINGING: //来电  
                endCall();//自动挂断  
                if(!numMap.containsKey(num)){//如果之前没有来电,把这个号码加入已经来电过的列表  
                    sendMes();  
                    numMap.put(num, null);  
                    peo ++;  
                    updateUi();//更新来电数目  
                }  
                break;  
            case TelephonyManager.CALL_STATE_OFFHOOK: //摘机(正在通话中)  
                break;  
            }  
        }  
        private void updateUi(){  
            if(count > 0){  
                tv.setVisibility(View.VISIBLE);  
            }  
            handler.sendEmptyMessage(0);
           // tv.setText("已拒接" + count + "个来电,共" + peo +"个人联系过您");  
        }  
        private void endCall() 
        {    
            Class<TelephonyManager> 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)    
            {       
            }            
        }    
          
        private void sendMes(){  
            //直接调用短信接口发短信  
            SmsManager smsManager = SmsManager.getDefault();  
            List<String> divideContents = smsManager.divideMessage(et.getText().toString());   
            for (String text : divideContents) {      
                smsManager.sendTextMessage(num, null, text, null, null);    
            }  
        }  
    }  

    public Handler handler = new Handler()
	{
		public void handleMessage(android.os.Message msg)
		{
			switch (msg.what)
			{
			// 接收的内容
			case 0:
				tv.setText("已拒接" + count + "个来电,共" + peo +"个人联系过您");
				break;
			default:
				break;
			}
		};
	};
}     
权限设置:
 <uses-permission android:name="android.permission.READ_PHONE_STATE" />    
 <uses-permission android:name="android.permission.CALL_PHONE"/> 
 <uses-permission android:name="android.permission.SEND_SMS"/> 




猜你喜欢

转载自blog.csdn.net/cugzhaolc/article/details/80446420