歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android 2.3以上自動接聽來電

因為Android2.3以上增加了對permission  android.permission.MODIFY_PHONE_STATE 的限制,2.3之前的通過反射機制調用ITelephone的能力的做法已經不適用

2.3上實現方式:

  1. public synchronized void answerRingingCall() {   
  2.         // 據說該方法只能用於Android2.3及2.3以上的版本上   
  3.         try {   
  4.             Intent localIntent1 = new Intent(Intent.ACTION_HEADSET_PLUG);   
  5.             localIntent1.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);   
  6.             localIntent1.putExtra("state", 1);   
  7.             localIntent1.putExtra("microphone", 1);   
  8.             localIntent1.putExtra("name", "Headset");   
  9.             sendOrderedBroadcast(localIntent1,   
  10.                     "android.permission.CALL_PRIVILEGED");   
  11.             Intent localIntent2 = new Intent(Intent.ACTION_MEDIA_BUTTON);   
  12.             KeyEvent localKeyEvent1 = new KeyEvent(KeyEvent.ACTION_DOWN,   
  13.                     KeyEvent.KEYCODE_HEADSETHOOK);   
  14.             localIntent2.putExtra("android.intent.extra.KEY_EVENT",   
  15.                     localKeyEvent1);   
  16.             sendOrderedBroadcast(localIntent2,   
  17.                     "android.permission.CALL_PRIVILEGED");   
  18.             Intent localIntent3 = new Intent(Intent.ACTION_MEDIA_BUTTON);   
  19.             KeyEvent localKeyEvent2 = new KeyEvent(KeyEvent.ACTION_UP,   
  20.                     KeyEvent.KEYCODE_HEADSETHOOK);   
  21.             localIntent3.putExtra("android.intent.extra.KEY_EVENT",   
  22.                     localKeyEvent2);   
  23.             sendOrderedBroadcast(localIntent3,   
  24.                     "android.permission.CALL_PRIVILEGED");   
  25.             Intent localIntent4 = new Intent(Intent.ACTION_HEADSET_PLUG);   
  26.             localIntent4.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);   
  27.             localIntent4.putExtra("state", 0);   
  28.             localIntent4.putExtra("microphone", 1);   
  29.             localIntent4.putExtra("name", "Headset");   
  30.             sendOrderedBroadcast(localIntent4,   
  31.                     "android.permission.CALL_PRIVILEGED");   
  32.         } catch (Exception e) {   
  33.             e.printStackTrace();   
  34.         }   
  35.     }  
Copyright © Linux教程網 All Rights Reserved