2014-01-21 45 views
3

如何打開自定義用戶界面上的回答和拒絕按鈕,我想顯示自定義用戶界面,而不是默認撥號。 我使用下面的代碼,但撥號器是開放的,我的行爲沒有被打開:中斷來電android

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.callintruptdemo" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="10" 
    android:targetSdkVersion="17" /> 
<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".IncomingCall"></activity> 

    <receiver android:name=".CallReceiver"> 
     <intent-filter > 
      <action android:name="android.intent.action.PHONE_STATE"></action> 
     </intent-filter> 
    </receiver> 
</application> 

我有一個廣播接收器監聽來電和接收器:

@Override 
public void onReceive(Context context, Intent intent) { 
     Log.d("CallReceiver","IncomingBroadcastReceiver: onReceive: "); 

     String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); 
     Log.d("CallReceiver","IncomingBroadcastReceiver: onReceive: " + state); 
     if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) 
     { 
      Intent i = new Intent(context, IncomingCall.class); 
      i.putExtras(intent); 
      i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

      context.startActivity(i); 
     } 
} 

和IncomingCall類的代碼是:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    super.onCreate(savedInstanceState); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL); 

    setContentView(R.layout.activity_main); 

    String number = getIntent().getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); 
    TextView text = (TextView)findViewById(R.id.text); 
    text.setText("Incoming call from " + number); 
} 

但我的自定義UI是沒有顯示。

我也想要我的用戶界面上的按鈕,以及如何通過點擊該按鈕來接收來電。 在此先感謝。

編輯: - 更新我的代碼後,我可以打開我的自定義用戶界面,現在我想通過單擊按鈕來接收呼叫。如何做到這一點任何幫助..

+0

也許你的設備已經選擇默認的撥號器總是接聽來電。做一個網絡搜索如何清除你的默認應用程序。 – npace

+0

我在模擬器上測試它,當我運行應用程序它顯示撥號程序,但是當我調試應用程序,它顯示我的自定義用戶界面。所以我失蹤。 – Ravi

+0

檢查此[鏈接] [1],[LINK1] [2],和[鏈接] [3] [1]:http://stackoverflow.com/questions/9256218/show-my-活動,而不是標準屏幕的來電 [2]:http://stackoverflow.com/questions/8699257/popup-over-incoming-call-screen [3]:http: //stackoverflow.com/questions/2486547/android-incoming-call-screen –

回答

4

首先做到以下幾點:

try { 
     Thread.sleep(1000); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

然後調用abortBroadcast()方法調用您的活動前,確保把優先在你的IntentFilter如下所示:

<intent-filter android:priority="99999" > 
      <action android:name="android.intent.action.PHONE_STATE" /> 
    </intent-filter> 

,並回答了自定義UI的來電請執行以下操作:

answerButton = (Button) findViewById(R.id.pickup); 
    answerButton.setOnClickListener(new OnClickListener() { 
     public void onClick(final View v) { 
      Intent answer = new Intent(Intent.ACTION_MEDIA_BUTTON); 
      answer.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK)); 
    context.sendOrderedBroadcast(answer, null); 

     } 
    }); 

,並拒絕呼叫做到以下幾點:

rejectButton= (Button) findViewById(R.id.pickup); 
    rejectButton= .setOnClickListener(new OnClickListener() { 
     public void onClick(final View v) { 
      Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON); 
    buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK)); 
    getApplicationContext().sendOrderedBroadcast(buttonDown, "android.permission.CALL_PRIVILEGED"); 


    } 
}); 

希望幫助

+0

嘗試你的代碼,但仍然相同的默認撥號器打開來電 – Ravi

+0

後使用您的代碼,並在調用我的自定義用戶界面之前添加一個延遲我能夠顯示我的用戶界面,現在我怎樣才能通過點擊按鈕來接聽電話。 – Ravi

+0

多數民衆贊成在一個新的問題:) plaese給我一個正確的答案,並開始一個新的問題。 –