Hide application icon
Asked Answered
S

5

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?

Sodom answered 6/12, 2011 at 10:39 Comment(1)
Can you little bit elaborate more?? Where you stuck at, making application run in Background or detecting key code? Or both??Bonesetter
K
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);
    }

}
Kalinda answered 25/7, 2012 at 4:20 Comment(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 menuAegospotami
@shujj : His utility class. :DInsulate
T
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" />
Thesis answered 10/5, 2014 at 17:33 Comment(0)
E
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).

Erichericha answered 16/12, 2011 at 18:23 Comment(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.Homomorphism
L
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)

Luganda answered 24/11, 2013 at 13:9 Comment(0)
G
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);
}

}
Graphitize answered 9/4, 2014 at 10:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.