Search code examples
androidhidden

how can i recall a hidden app from phone dialer?


My Final project requires that app must be hidden,and never opened again unless entering some code in phone dialer (ex: *123#) can you help me guys to do that task?


Solution

  • This is a bit tricky and it has its up and downs, but what you need to do basically is:

    1. On app install, you need to programmatically disable the app Icon so you cannot open it manually.
    2. Have a BroadcastReceiver registered with the PROCESS_OUTGOING_CALLS intent filter (don't forget to set the uses-permissions).
    3. In the receiver, listen for every dialed number and when it matches yours you need to activate the App Icon again and then you start the activity with possibly extra data to handle it later.
    4. After processing the data in your activity remember to deactivate the icon again.

    To programmatically disable the icon use:

    PackageManager packageManager = getPackageManager();
                ComponentName componentName = new ComponentName(this, MainActivity.class);
                packageManager.setComponentEnabledSetting(
                        componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP
                );
    

    To enable it:

     PackageManager packageManager = context.getPackageManager();
                    ComponentName componentName = new ComponentName(context, MainActivity.class);
                    packageManager.setComponentEnabledSetting(
                            componentName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP
                    );
    

    In your receiver to get the dialed number you need to use:

    if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
         String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
    
         // Validate and start your activity here
         // To start an activity from a receiver you need to use the flag FLAG_ACTIVITY_NEW_TASK in your intent
    }
    

    Note: After hiding the icon programmatically you might want to finish() the activity so it closes automatically at first run.

    P.S I have a working sample of this, so rest assured as I have tested it actually works, sadly I cannot spoon feed you in your final project. Don't hesitate to ask anything tho. Good luck