Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to accept an incoming call by clicking a button?

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 code is:

 @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:

<uses-permission android:name="android.permission.CALL_PHONE" />

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 in call icon 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?

like image 712
Armstrong Avatar asked May 06 '10 09:05

Armstrong


2 Answers

Add the category "android.intent.category.DEFAULT" (Intent.CATEGORY_DEFAULT)

like image 76
gvaish Avatar answered Nov 15 '22 01:11

gvaish


The intent android.intent.action.ANSWER is somehow not working as expected. There is a workaround by emulating the bluetooth button to answer the incoming call. You can see an example from auto-answer project.

like image 39
LewCPE Avatar answered Nov 15 '22 01:11

LewCPE