android 自動掛斷來電

原文: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();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章