Search code examples
androidtaskmultitaskingminimizein-call

Android How can I Minimize In Call Screen & Move my own task forward - Android 10 (2.3.3)


It feels like this question has been asked allot, but non of them fits my needs an non of the solutions work for me.

I have an application with a class extending to PhoneStateListener to listen for the following states:

  • CALL_STATE_RINGING (Here I set a global string variable with the incoming Phone Number)
  • CALL_STATE_OFFHOOK (Here I want to move my Task/Process to the Front for the user to see my application with the callers details on the screen, and not the In Call screen)

I need a way to minimize the In Call Screen (into the Notifications Area) I know it can be done (have Proof) but I do not know how to do it After it is minimized, I should be able to move my task to the front which was previously moved to the back.

My application starts up, the user logs in and by keep pressing the back button, the user stays logged in but the application is moved to the background. If an incoming call occurs, I want to see if the callers phone number exists in my application (The application keeps a couple of users info), then go to the users info after the In Call Screen is minimized.

When I run this code in the CALL_STATE_OFFHOOK state, the call is minimized and the Map Loads on Full Screen (The Proof)

public String IncomingTelephoneNumber;
private class CallStateListener extends PhoneStateListener {

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        switch (state) {
            case TelephonyManager.CALL_STATE_RINGING: {
                IncomingTelephoneNumber = incomingNumber;
                break;
            }
            case TelephonyManager.CALL_STATE_OFFHOOK: {
                Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
                intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
                activity.startActivity(intent);
                break;
            }
        }
    }
}

It is not possible to get hold of the Maps Source Code. In the same way I want to do this, but with my own application, I Tried this, but does not work, the In Call Screen is still in front of the application (or the application isn't called at all). The code does not break or anything, it is just that my app does not come forward.

public String IncomingTelephoneNumber;
private class CallStateListener extends PhoneStateListener {

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        switch (state) {
            case TelephonyManager.CALL_STATE_RINGING: {
                IncomingTelephoneNumber = incomingNumber;
                break;
            }
            case TelephonyManager.CALL_STATE_OFFHOOK: {
                try {
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.addCategory(Intent.CATEGORY_LAUNCHER);
                    intent.setFlags(270532608);//This is the flag which is used from the Launcher, LogCat helped here //flg=0x10200000
                    intent.setClassName(activity.getPackageName(), Splash.class.getCanonicalName());
                    activity.startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }
}

I have tried other ways to bring the application back to the front, but does not work.

I saw this post, but no solution what so ever.

I use Android Version 10 as it is required by the devices being used (Samsung P-1000 Tablets, Android 2.3.3 is the latest official version)

Any help would be much appreciated. It may be a train smash or something very simple which needs to be called. But I would like this to be done, the same way as the maps do it.

EDIT: I came across a solution to minimize the In Call Screen (Not the best Solution, but it works)

public String IncomingTelephoneNumber;
private class CallStateListener extends PhoneStateListener {

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        switch (state) {
            case TelephonyManager.CALL_STATE_RINGING: {
                IncomingTelephoneNumber = incomingNumber;
                break;
            }
            case TelephonyManager.CALL_STATE_OFFHOOK: {
                try {
                    //This will simulate pressing the Home button
                    //Thus Minimizing the In Call Screen
                    Intent startMain = new Intent(Intent.ACTION_MAIN);
                    startMain.addCategory(Intent.CATEGORY_HOME);
                    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    activity.startActivity(startMain);

                    //This is the part still not working Correctly
                    //Moving my Task/Process to the Front for the user to see
                    //This is Android 10, I still can not find a solution to move task to front
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.addCategory(Intent.CATEGORY_LAUNCHER);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.setClassName(activity, Splash.class.getCanonicalName());
                    activity.startActivity(intent);
                } catch (Exception e) {
                    Log.v("", "e: " + e.getMessage());
                    e.printStackTrace();
                }
                break;
            }
        }
    }
}

Solution

  • I Found the Solution for my Particular problem, After playing around with allot of different settings and reading forums, this is what I came up with and works on Android GingerBread 2.3.3 (version 10)

    To Minimize the call and go to the home screen, I have done this

    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    activity.startActivity(startMain);
    

    I have decided not to use the above, only use the following which basically does precisely what I need (Solution works with two parts, Intent code and the code in the Android Manifest file)

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    
    //Here the activity class in Question "JobDetail"
    intent.setClassName(activity.getPackageName(), JobDetail.class.getCanonicalName());
    
    //These extras I add and then use them in my
    //JobDetail class to show the specific details for the caller
    intent.putExtra("JobId", JobId);
    intent.putExtra("FromCall", "true");
    activity.startActivity(intent);
    

    Manifest part

    <activity android:name="JobDetail" android:configChanges="orientation|keyboardHidden"
        android:taskAffinity="" android:launchMode="singleInstance"
        android:alwaysRetainTaskState="true" />
    

    Under the specific Activity Class in the Manifest file, which you want to bring to front, in my case showing it over the InCallScreen, add the following three properties/tags

    • android:taskAffinity="" (I am not sure if this does anything, but I leave it there)
    • android:launchMode="singleInstance" (I think this will launch a brand new instance of the Activity, but with only this option and the Intent above, I found that it restarts the whole application and therefore add the last property/tag)
    • android:alwaysRetainTaskState="true" (This makes sure it opens the Activity Class directly, and not reset the entire application)

    After this, handle your back keys in case the activity class you are launching is deep in the application within another parent activity and so on.

    Also found that using the moveTaskToBack(true); to minimize your application does not work well in Gingerbread. I do not know all the technical detail behind this, but this tells me that the application in question is moved right to the back of the stack and therefor finding that getting your task to the front is not an easy adventure. Rather use the following intent call (Simulates pressing the home button, light-hide your application from the user).

    @Override
    public void onBackPressed() {
        //moveTaskToBack(true);
        Intent startMain = new Intent(Intent.ACTION_MAIN);
        startMain.addCategory(Intent.CATEGORY_HOME);
        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(startMain);
        //Show toast to notify the user that your application is still running in the background
        return;
    }
    

    I hope this helps someone also stuck with the same problem as me with old hardware and old software.