现在的位置: 首页 > 综合 > 正文

android 自动挂断来电

2014年02月19日 ⁄ 综合 ⁄ 共 1997字 ⁄ 字号 评论关闭

原文:http://blog.163.com/wu_zefeng/blog/static/1826291752011312114420975/ 

android的新版本已经把Phone类给隐藏起来了,想要用代码实现挂断电话,就必须通过AIDL才行,

第一步:在程序中新建一个包,包名必须为:com.android.internal.telephony,因为要使用aidl,

第二步:在这个包里面新建一个名为ITelephony.aidl的文件,然后在文件里面写入代码:

package com.android.internal.telephony;
interface ITelephony{
boolean endCall();
void answerRingingCall();
}

然后保存,eclipse会自动在gen文件夹下生成一个ITelephony.java的类。

主程序的代码如下:

package ling.Phonemanager;

import java.lang.reflect.Method;

import android.app.Activity;
import android.os.Bundle;
import android.os.RemoteException;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

import com.android.internal.telephony.ITelephony;
public class Phonemanager extends Activity {
    /** Called when the activity is first created. */
    private ITelephony  iTelephony;
    private TelephonyManager manager;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        phoner();
        manager.listen(new PhoneStateListener(){

   @Override
   public void onCallStateChanged(int state, String incomingNumber) {
    // TODO Auto-generated method stub
    super.onCallStateChanged(state, incomingNumber);
    switch(state){
    //判断是否有电话接入
    case 1:
     try {
      //当电话接入时,自动挂断。
      iTelephony.endCall();
      System.out.println("uncall");
     } catch (RemoteException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
   }
         
        }, PhoneStateListener.LISTEN_CALL_STATE);

    }
    public void phoner(){
     manager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
        Class <TelephonyManager> c = TelephonyManager.class;  
         Method getITelephonyMethod = null;  
         try {  
                getITelephonyMethod = c.getDeclaredMethod("getITelephony", (Class[])null);  
                getITelephonyMethod.setAccessible(true);  
          iTelephony = (ITelephony) getITelephonyMethod.invoke(manager, (Object[])null);  
         } catch (IllegalArgumentException e) {  
               e.printStackTrace();  
         } catch (Exception e) {  
              e.printStackTrace(); 

         } 
    }
}

只要在电话接入时,再加上一个判断电话号码是否是黑名单的功能,就可以做成一个黑名单的程序了,获取电话号码的函数是:getLine1Number();

抱歉!评论已关闭.