How to make a phone call in android and come back to my activity when the call is done?

后端 未结 20 2505
北恋
北恋 2020-11-22 12:59

I am launching an activity to make a phone call, but when I pressed the \'end call\' button, it does not go back to my activity. Can you please tell me how can I launch a c

20条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 13:40

    When starting your call, it looks fine.

    There is a difference between android 11+ and down in bringing your app to the front though.

    Android 10 or less you need to start a new intent, android 11+ you simply use BringTaskToFront

    In the call state IDLE:

    if (Build.VERSION.SDK_INT >= 11) {
        ActivityManager am = (ActivityManager) activity.getSystemService(Activity.ACTIVITY_SERVICE);
        am.moveTaskToFront(MyActivity.MyActivityTaskId, ActivityManager.MOVE_TASK_WITH_HOME);
    } else {
        Intent intent = new Intent(activity, MyActivity.class);
        activity.startActivity(intent);
    }
    

    I set the MyActivity.MyActivityTaskId when making the call on my activity like so, it this doesnt work, set this variable on the parent activity page of the page you want to get back to.

    MyActivity.MyActivityTaskId = this.getTaskId();
    

    MyActivityTaskId is a static variable on my activity class

    public static int MyActivityTaskId = 0;
    

    I hope this will work for you. I use the above code a bit differently, I open my app as soon as the call is answered sothat the user can see the details of the caller.

    I have set some stuff in the AndroidManifest.xml as well:

    /*Dont really know if this makes a difference*/
    
    

    and permissions:

    
    
    

    Please ask questions if or when you get stuck.

提交回复
热议问题