12

I am doing an Android application. I want to hide the application icon in the emulator and I want to start my application by pressing some numbers, for instance 456#. Is there a way to do this?

1
  • 1
    Can you little bit elaborate more?? Where you stuck at, making application run in Background or detecting key code? Or both??
    – Prasham
    Dec 6, 2011 at 12:46

5 Answers 5

19

To Hide app icon from launcher programatically you can do this

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

To launch app by pressing number first add folowing permission in mainfest file

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

Then register receiver

<receiver android:name=".LaunchAppViaDialReceiver"> 
    <intent-filter> 
    <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
    </intent-filter> 
</receiver>

Then create a receiver class

  public class LaunchAppViaDialReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Bundle bundle = intent.getExtras();
    if (null == bundle)
        return;
    String phoneNubmer = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
             //here change the number to your desired number
    if (phoneNubmer.equals("12345")) {
        setResultData(null);
        Gaurdian.changeStealthMode(context,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
        Intent appIntent = new Intent(context, LauncherActivity.class);
        appIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(appIntent);
    }

}
2
  • your code is so help full but i have a problem my app icon is still visible the only way it vanishes away is to restart the phone.. is there a to refresh the menu Sep 26, 2013 at 19:32
  • @shujj : His utility class. :D Sep 12, 2016 at 8:35
14

If you want to hide the app icon it's a good idea to show the icon first and let the user know how to start the app once the icon is gone. First create an activity-alias in the manifest and move your intent filter there. This way you can disable the icon without disabling the activity.

<activity
    android:name=".MainActivity"
    android:label="@string/app_name" >
</activity>
<activity-alias
    android:name=".Launcher" 
    android:targetActivity=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter> 
</activity-alias>

Get the component name of the launcher alias using your package name:

private static final ComponentName LAUNCHER_COMPONENT_NAME = new ComponentName(
            "your.package.name", "your.package.name.Launcher");

You can check if it's already disabled...

private boolean isLauncherIconVisible() {
    int enabledSetting = getPackageManager()
            .getComponentEnabledSetting(LAUNCHER_COMPONENT_NAME);
    return enabledSetting != PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
}

...and disable it when appropriate after giving the user information:

private void hideLauncherIcon() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Important!");
    builder.setMessage("To launch the app again, dial phone number 12345.");
    builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            getPackageManager().setComponentEnabledSetting(LAUNCHER_COMPONENT_NAME,
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                    PackageManager.DONT_KILL_APP);
        }
    });
    builder.setIcon(android.R.drawable.ic_dialog_alert);
    builder.show();
}

To launch from the dialer create a broadcast receiver:

public class LaunchViaDialReceiver extends BroadcastReceiver {

    private static final String LAUNCHER_NUMBER = "12345";

    @Override
    public void onReceive(Context context, Intent intent) {
        String phoneNubmer = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        if (LAUNCHER_NUMBER.equals(phoneNubmer)) {
            setResultData(null);
            Intent appIntent = new Intent(context, MainActivity.class);
            appIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(appIntent);
        }
    }
}

Add it to the manifest:

<receiver android:name=".LaunchViaDialReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
    </intent-filter>
</receiver>

And add the permission to the manifest:

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

The answer to the first part of your question, try this code:

PackageManager pm = getApplicationContext().getPackageManager(); 
pm.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

Your application will not be visible, but the user can still find it in the Settings >> Applications >> Manage Application

This answer may also be helpful for you.

Please do not forget to post your answer here, if you have already achieved the functionality(pressing some number & opening our application).

1
  • I tried the code you listed on an phone with Android 2.21. Although it did not hide the app icon in the drawer, it did something. Now when I click on the icon, it says "Application is not installed on your phone". Thanks.
    – timeon
    Jun 27, 2012 at 15:59
4

Note that the solution:

PackageManager pm = getApplicationContext().getPackageManager(); 
pm.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

will make the app NOT upgradeable from google play as the OS will not find the package after this component disabling and will not able to re-install it, unless the app is not manullay uninstalled (which is not a user friendly behaviour)

3
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

        hideapplication();
}


   private void hideapplication() {
// TODO Auto-generated method stub
     PackageManager pm = getApplicationContext().getPackageManager();
   pm.setComponentEnabledSetting(getComponentName(),                           PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.