Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2108658
  • 博文数量: 413
  • 博客积分: 10926
  • 博客等级: 上将
  • 技术积分: 3862
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-09 18:14
文章分类

全部博文(413)

文章存档

2015年(5)

2014年(1)

2013年(5)

2012年(6)

2011年(138)

2010年(85)

2009年(42)

2008年(46)

2007年(26)

2006年(59)

分类: Java

2009-03-11 16:05:16

  1. Listen outgoing call
    Register a broadcast receiver with action android.intent.action.NEW_OUTGOING_CALL,
    but please request to use permission android.permission.PROCESS_OUTGOING_CALLS. You can get outgoing phone number with following statement in method BroadcastReceiver.onReceive(xxx);
    String strPhoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
  2. Listen incoming call
    Use TelephonyManager and PhoneStateListener
    import android.app.Activity;
    import android.content.Context;
    import android.telephony.PhoneStateListener;
    import android.telephony.TelephonyManager;

    public class Telephony extends Activity
    {
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            TelephonyManager mTelephonyMgr =
    (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
            mTelephonyMgr.listen(new TeleListener(),
    PhoneStateListener.LISTEN_CALL_STATE);

      setContentView(xxxxxxxx);
    }

    class TeleListener extends PhoneStateListener
    {
        public void onCallStateChanged(int state, String incomingNumber)
        {
            super.onCallStateChanged(state, incomingNumber);
            switch (state)
            {
                case TelephonyManager.CALL_STATE_IDLE: //The call is terminated
                //CALL_STATE_IDLE;
                   break;
                case TelephonyManager.CALL_STATE_OFFHOOK: //The call is answered
                   //CALL_STATE_OFFHOOK;
                   break;
                case TelephonyManager.CALL_STATE_RINGING: //The new call is comming
                   //CALL_STATE_RINGING
                   break;
                default:
                   break;
             }
          }
        }
    }
  3. Implement Call Filter/Firewall
    • Please refer to:
      %20package:%5C.googlecode%5C.com&d=0
    • Sample:
      • Prepare ITelephony.aidl
        Download iTelephony.aidl from
        then add that file to src/com/android/internal/telephony/ of your project
      • Add permissions to AndroidManifest.xml



      • Write Code
        import java.lang.reflect.Method;
        import com.android.internal.telephony.ITelephony;
        import android.telephony.*;

        ......

        private TelephonyManager m_telephonyManager;
        private ITelephony m_telephonyService;
        private PhoneStateListener m_phoneStateListener;
        private AudioManager m_audioManager;

        ....

        m_telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);


        Class c = Class.forName(m_telephonyManager.getClass().getName());
        Method m = c.getDeclaredMethod("getITelephony");
        m.setAccessible(true);
        m_telephonyService = (ITelephony)m.invoke(m_telephonyManager);

        //You can also replace above 4 lines of statements to
        //m_telephonyService = ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELE

        m_audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        m_telephonyManager.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);



        ....


        class MyPhoneStateListener extends PhoneStateListener
        {
            public void onCallStateChanged(int state, String incomingNumber)
            {
                m_audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);

                switch (state)
                {
                    case TelephonyManager.CALL_STATE_IDLE:
                       m_audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                       break;
                    case TelephonyManager.CALL_STATE_OFFHOOK:
                       //CALL_STATE_OFFHOOK;
                       break;
                    case TelephonyManager.CALL_STATE_RINGING:
                       if (xx) // if incomingNumber need to be blocked
                       {
                           m_telephonyService.endCall();
                       }
                       else
                       {
                           m_audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                       }
                       break;
                    default:
                       break;
                 }
              }
            }
        }
      • xxxxx
    • xxxx
  4. Replace Incoming call screen
    Sample: Android Caller ID

  5. xxx

 

阅读(4798) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~