How to accept an incoming call by clicking a button?

Posted by upright on Stack Overflow See other posts from Stack Overflow or by upright
Published on 2010-05-06T09:31:58Z Indexed on 2010/05/06 9:38 UTC
Read the original article Hit count: 220

Filed under:

HI, all!

I'm trying to implement my own phone call handling UI.

What I want to do is, if a call comes in, the incoming telephone number and a picture are displayed, and, if I press a button, the incoming call will be accepted/answered.

The related codes are:

@Override public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
answerButton = (Button) findViewById(R.id.pickup);
answerButton.setOnClickListener(new OnClickListener() {
    public void onClick(final View v) {
        Intent intent = new Intent("android.intent.action.ANSWER");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);          
    }
});

Sadly, the code does not work. At first, an exception is thrown if I press my answer button: ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.ANSWER

Then I added an entry in the AndroidManifest.xml:

I run the app again, there is no exception anymore. However, I doubt the incoming call is not really accepted. Because if the press the Android's screen answer button (green button), the incoming call is accepted and a green button is also displayed on the upper left corner of the emulator screen, while my app doesn't.

I also read the Phone app's source code in android source. There is method such as acceptCall() in the Phone class. But these codes seem difficult for me to use, because there are many imports declaration in the code, such as :

import com.android.internal.telephony.Call; import com.android.internal.telephony.CallStateException; import com.android.internal.telephony.CallerInfo; import com.android.internal.telephony.CallerInfoAsyncQuery; import com.android.internal.telephony.Connection; import com.android.internal.telephony.MmiCode; import com.android.internal.telephony.Phone;

And, if I add these imports in my code, there will be too many errors, such as : "The import com.android.internal.telephony cannot be resolved"

What is the right and simple way for my problem?

Thanks in advance!

© Stack Overflow or respective owner

Related posts about android