How to accept an incoming call by clicking a button?

↘锁芯ラ 提交于 2020-01-11 04:49:06

问题


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?


回答1:


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




回答2:


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.




回答3:


You need to create a broadcast receiver in which you will get the event when your phone is ringing and after that you can launch your desired activity.You can not replace the default incoming call screen until using CUSTOM ROM. And do not forget to set the priority in broadcast receiver in manifest file. Once you get the event you can use the object of ITelephony by using reflection.And that can provide you methods to answering or rejecting the call.




回答4:


This is possible using the com.android.internal.telephony package, but you have to find someway for using this methods in eclipse and your app has to be compiled as a system app using the android source code.




回答5:


Change your accept call method by this:

public static void acceptCall(Context context) 
{
    Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
    buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, 
      new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
    context.sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
} 


来源:https://stackoverflow.com/questions/2779991/how-to-accept-an-incoming-call-by-clicking-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!