title: Hide Application on button click and unhide via dialer
body: <p>I want to hide my application in android on button click and unhide it by dialing a particular number (say dialing 12345 will unhide it). </p>
  
  <p>I am using Android Studio 1.1.0. Targeted API level is 21.</p>
  
  <p>I have tried every single code available on stackoverflow.</p>
  
  <p>Please suggest me a way or a working code...</p>
  
  <p>to hide icon i used :</p>
  
  <pre><code>PackageManager pm = getPackageManager();
  ComponentName componentName = new ComponentName(this,
          com.redski.hideunhide.MainActivity.class);
  pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
          PackageManager.DONT_KILL_APP);
  </code></pre>
  
  <p>Other functions that i made are:</p>
  
  <pre><code>private boolean isLauncherIconVisible() {
      int enabledSetting = getPackageManager()
              .getComponentEnabledSetting(LAUNCHER_COMPONENT_NAME);
  
      return enabledSetting != PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
  }
  
  private void hideLauncherIcon() {
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setTitle("Important!");
      builder.setMessage("To launch the app again, dial phone number *#55555*");
      builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
              pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
              PackageManager.DONT_KILL_APP);
          }
          });
      builder.setIcon(android.R.drawable.ic_dialog_alert);
      builder.show();
  }
  </code></pre>
  
  <p>TO unhide via dialer i made a LaunchViaDialReceiver class which is as follows:</p>
  
  <pre><code>package com.redski.hideunhide;
  
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.content.pm.PackageManager;
  
  public class LaunchViaDialReceiver extends BroadcastReceiver {
  
      private static final String LAUNCHER_NUMBER = "*#55555*";
      @Override
      public void onReceive(Context context, Intent intent) {
          //TODO Auto-generated method stub
          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);
          }
      }
  }
  </code></pre>
  
  <p>manifest file :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.redski.hideunhide" &gt;
  
      &lt;uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" /&gt;
  
  
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name=".MainActivity"
              android:label="@string/app_name" &gt;
  
          &lt;/activity&gt;
          &lt;activity-alias
              android:name=".Launcher"
              android:targetActivity=".MainActivity"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity-alias&gt;
  
  
  
          &lt;receiver android:name=".LaunchViaDialReceiver" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.NEW_OUTGOING_CALL" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>