Hide application icon

前端 未结 5 1786
广开言路
广开言路 2020-12-01 02:58

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 the

相关标签:
5条回答
  • 2020-12-01 03:41

    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" />
    
    0 讨论(0)
  • 2020-12-01 03:52

    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)

    0 讨论(0)
  • 2020-12-01 03:53

    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).

    0 讨论(0)
  • 2020-12-01 03:57

    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);
        }
    
    }
    
    0 讨论(0)
  • 2020-12-01 03:59
    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);
    }
    
    }
    
    0 讨论(0)
提交回复
热议问题