如何从启动器消失应用程序图标?

时间:2017-11-16 06:48:53

标签: android

我想从启动器中消失我的应用程序图标,应用程序会在一段时间后自动重启。 我在下面隐藏了图标代码:

 try {
       PackageManager p = context.getPackageManager();
        ComponentName componentName = new ComponentName("com.package.name", "com.class.name");
        p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    } catch (Exception e) {
        Log.d("ERROR@@", "" + e.toString());
    }

这是一段时间后重新打开我的应用的代码

public void initializingTimerTask() {
    timerTask = new TimerTask() {
        @Override
        public void run() {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    if (mIsInForegroundMode == false) {
                        Intent LaunchIntent=getPackageManager().getLaunchIntentForPackage("com.package.name");
                        LaunchIntent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(LaunchIntent);
                    } else {
                    }
                }
            });
        }
    };
}

错误:

 11-16 14:10:02.802 18503-18503/com.package.newproject E/AndroidRuntime: FATAL EXCEPTION: 
    main Process: com.package.newproject, PID: 18503 java.lang.NullPointerException 
    at com.mspatel.package.newproject.AutoOpenAppService$1$1.run(AutoOpenAppService.java:101) 
    at android.os.Handler.handleCallback(Handler.java:743) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:136) 
    at android.app.ActivityThread.main(ActivityThread.java:5212) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 
    at dalvik.system.NativeStart.main(Native Method)

但是当转向重启应用程序应用程序崩溃时,请帮助我。

1 个答案:

答案 0 :(得分:0)

如果我理解正确而不是在这里。 要隐藏图标,请使用以下命令:

PackageManager p = getPackageManager();
    ComponentName componentName = new ComponentName(this, com.sstudio.sysUtil.TheActivity.class); // activity which is first time open in manifiest file which is declare as <category android:name="android.intent.category.LAUNCHER" />
    p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

请记住,在再次打开应用程序之前,您需要首先取消隐藏图标,然后打开应用程序,否则它将崩溃。这样做:

PackageManager p = context.getPackageManager();
        ComponentName componentName = new ComponentName(context, TheActivity.class);
        p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); // to unhide the icon and to prevent the app from crashing
        Intent i = new Intent(context, TheActivity.class);// use your own intent as required
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);// and add flags as required
        context.startActivity(i);//and open activity

我遇到了同样的问题。这确实解决了我的问题希望它也能解决你的问题。

修改 改变你的代码:

public void initializingTimerTask() {
timerTask = new TimerTask() {
    @Override
    public void run() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if (mIsInForegroundMode == false) {
                    Intent LaunchIntent=getPackageManager().getLaunchIntentForPackage("com.package.name");
                    LaunchIntent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(LaunchIntent);
                } else {
                }
            }
        });
    }
};}

用这个:

 public void initializingTimerTask() {
    timerTask = new TimerTask() {
        @Override
        public void run() {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    if (mIsInForegroundMode == false) {
PackageManager p = getPackageManager();
        ComponentName componentName = new ComponentName(getApplicationContext(), TheActivity.class);
        p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);        
                        Intent LaunchIntent=getPackageManager().getLaunchIntentForPackage("com.package.name");
                        LaunchIntent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(LaunchIntent);
                    } else {
                    }
                }
            });
        }
    };}