EX06_06.java
上传用户:vip_99
上传日期:2021-03-27
资源大小:61159k
文件大小:3k
源码类别:

android开发

开发平台:

Java

  1. package irdc.ex06_06;
  2. import android.app.Activity;
  3. import android.content.ContentResolver;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.graphics.Color;
  7. import android.os.Bundle;
  8. import android.provider.Contacts;
  9. import android.telephony.PhoneStateListener;
  10. import android.telephony.TelephonyManager;
  11. import android.widget.TextView;
  12. public class EX06_06 extends Activity
  13. {
  14.   private TextView myTextView1;
  15.   /** Called when the activity is first created. */
  16.   @Override
  17.   public void onCreate(Bundle savedInstanceState)
  18.   {
  19.     super.onCreate(savedInstanceState);
  20.     setContentView(R.layout.main);
  21.     
  22.     myTextView1 = (TextView) findViewById(R.id.myTextView1);
  23.     
  24.     /* 添加自己实现的PhoneStateListener */
  25.     exPhoneCallListener myPhoneCallListener = 
  26.     new exPhoneCallListener();
  27.     
  28.     /* 取得电话服务 */
  29.     TelephonyManager tm =
  30.     (TelephonyManager) this.getSystemService
  31.     (Context.TELEPHONY_SERVICE);
  32.     
  33.     /* 注册电话通信Listener */
  34.     tm.listen
  35.     (
  36.       myPhoneCallListener,
  37.       PhoneStateListener.LISTEN_CALL_STATE
  38.     );
  39.   }
  40.   /* 内部class继承PhoneStateListener */
  41.   public class exPhoneCallListener extends PhoneStateListener
  42.   {
  43.     /* 重写onCallStateChanged
  44.     当状态改变时改变myTextView1的文字及颜色 */
  45.     public void onCallStateChanged(int state, String incomingNumber)
  46.     {
  47.       switch (state)
  48.       {
  49.         /* 无任何状态时 */
  50.         case TelephonyManager.CALL_STATE_IDLE:
  51.           myTextView1.setTextColor
  52.           (
  53.             getResources().getColor(R.drawable.red)
  54.           );
  55.           myTextView1.setText("CALL_STATE_IDLE");
  56.           break;
  57.         /* 接起电话时 */
  58.         case TelephonyManager.CALL_STATE_OFFHOOK:
  59.           myTextView1.setTextColor
  60.           (
  61.             getResources().getColor(R.drawable.green)
  62.           );
  63.           myTextView1.setText("CALL_STATE_OFFHOOK");
  64.           break;
  65.         /* 电话进来时 */
  66.         case TelephonyManager.CALL_STATE_RINGING:
  67.           getContactPeople(incomingNumber);
  68.           break;
  69.         default:
  70.           break;
  71.       }
  72.       super.onCallStateChanged(state, incomingNumber);
  73.     }
  74.   }
  75.   private void getContactPeople(String incomingNumber)
  76.   {
  77.     myTextView1.setTextColor(Color.BLUE);
  78.     ContentResolver contentResolver = getContentResolver();
  79.     Cursor cursor = null;
  80.     /* cursor里要放的字段名称 */
  81.     String[] projection = new String[]
  82.     {
  83.       Contacts.People._ID,
  84.       Contacts.People.NAME,
  85.       Contacts.People.NUMBER
  86.     };
  87.     /* 用来电电话号码去找该联系人 */
  88.     cursor = contentResolver.query
  89.     (
  90.       Contacts.People.CONTENT_URI, projection,
  91.       Contacts.People.NUMBER + "=?",
  92.       new String[]
  93.       {
  94.         incomingNumber
  95.       },
  96.       Contacts.People.DEFAULT_SORT_ORDER
  97.     );
  98.     /* 找不到联系人 */
  99.     if (cursor.getCount() == 0)
  100.     {
  101.       myTextView1.setText("unknown Number:" + incomingNumber);
  102.     }
  103.     else if (cursor.getCount() > 0)
  104.     {
  105.       cursor.moveToFirst();
  106.       /* 在projection这个数组里名字是放在第1个位置 */
  107.       String name = cursor.getString(1);
  108.       myTextView1.setText(name + ":" + incomingNumber);
  109.     }
  110.   }
  111. }