How to delete icon from Homescreen programmatically on Android - android

i am use this code for delete icon from homescreen:
PackageManager p = getPackageManager();
p.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
But after click button "Delete Icon", app closes, I need to remove the icon and use the application on.

You can't disable the app and still continue using it.
Your code above puts the app into disabled state. The PackageManager will not allow the app to run unless it is re-enabled.
If you want your app to still run, but not have an icon in the all apps drawer of your homescreen, you can by removing the <category android:name="android.intent.category.LAUNCHER" /> from your activity reference in your AndroidMainfest.xml:
<activity android:name=".HiddenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
The app will not appear in the all apps drawer of your home screen, but can be launched from other apps using the following code:
Intent intent = new Intent();
intent.setClassName("app_package_name", "app_package_name.HiddenActivity");
startActivity(intent);
Note: The app with the HiddenActivity must have been started by the user after installing, otherwise the app will be in Stopped state and can't be launched from another application.

Related

Home Button in Home Screen App android

I am working on app which will serve as Home Screen Lock. The app will support only 4.1 and above versions. The issue is handling the Home Button. I understand that we cannot override/disable Home Button, so it has to be handled a different way. I have been struggling for almost 2 weeks and tried many umpteenth ways but none of them working as expected. Main Activity is the one which will be on top and user need to enter his set password here to unlock the device. I have set my MainActivity as Home so the user is prompted to select between default launcher and my app. So expected action is to select my app as Launcher 'always'. Also I have set an flag in broadcast receiver whenever MainAcitivity is started. This flag is used in MainActivity to launch system Launcher in case MainActivity isn't started by broadcast receiver.
Everything is working fine till this point and approach. Issue is when I click on my app to change password or access other features, it doesn't open the app and opens the system launcher. The reason is it checks the flag in Main Activity and since it is not started by broadcast receiver, it doesn't launch MainActivity now and starts the system launcher. Below are code snippets:
Manifest File:
<activity
android:name=".MainActivity"
android:excludeFromRecents="true"
android:label="#string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</activity>
My Broadcast Receiver
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF) || action.equals(Intent.ACTION_USER_PRESENT)) {
AppSession.getInstance().setReceiverCall(true);
MainActivity.isStarted = true;
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
context.startActivity(i);
}
Main Activity where I check the flag and start the system launcher. I amy doing this on onResume
if (!MainActivity.isStarted) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(mPackageName, mName));
startActivity(intent);
}
mPackageName & mName are system launcher name.
I tried other approaches as well but only one of the thing is resolved. Either home button is handled correctly and then my app isn't launched. If app is launched then on Home button press from anywhere it always brings my MainActivity on top.
I know this is doable as its done in many ScreenLock apps but somehow I am missing something. Please advise.

Home Button that doesn't work only while my own app is in foreground

I have created a Home Screen replacement app. As per some of the suggestions here on SO, I approached this by creating the following two activities:
<activity
android:name=".Activities.GhostLauncherActivity"
android:enabled="false"
android:label="#string/title_activity_ghost_launcher">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity
android:name=".Activities.HomeScreenActivity"
android:enabled="true"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
Then toggling their enabled state as follows:
ComponentName component = new ComponentName(this, GhostLauncherActivity.class);
ComponentName secComponent = new ComponentName(this, HomeScreenActivity.class);
packageManager.setComponentEnabledSetting(component, packageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
packageManager.setComponentEnabledSetting(secComponent, packageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(intent);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
packageManager.setComponentEnabledSetting(component, packageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
This works like a charm. Every time I run the Java code above, the user is prompted to select a Home Screen app, with mine listed as one of the options. The HomeScreenActivity is itself a launcher screen: there is some GridView of apps that can be launched.
As per my question: when an app is launched from the Home Screen, pressing Home will return you to HomeScreenActivity. Perfect.
However, you can also access the SettingsActivity from within the Home Screen. Simple enough, the following code exists somewhere:
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
Now when I press the Home Button, the first time everything works fine...but the second time I go to settings, the Home button does not work. My app remains on the Settings screen. Even setting aside the the fact that it worked the first time, what could cause the Home button to stop working in my Home Screen replacement app? I have selectively commented out chunks of code from both Home Screen and Settings, exhaustively to check if any particular code chunk was responsible: none seem to be the culprit.
This error seems to be happening at some higher level and I can not figure out what that is. I am running Android 5.0 on my device. Has anyone experienced anything similar and if so, how can I address this issue or where else should I search for fixes within the code?
I was having the exact same problem; trying to launch my own activity from my home screen app. I found that adding the following line to my HOME activity and the sub-activity in the manifest fixed it:
android:launchMode="singleInstance"

how to make the Launcher-Pick-Up popup window showing?

I have an Launcher app installed. And user has choose another launcher as default, that means when pressing HOME the default launcher will come to front.
I wanna supply user with an convenience of reseting default launcher. Such as a button clicking in my launcher's UI will make Launcher-Pick-Up popup window showing.
Go Launcher can do that(in Go's setting view). it seems Go Launcher does something like "PackageManager.clearPackagePreferredActivities("com.android.launcher")" to clear the prefered launcher activity!
how to achive that ?
launcher can only clear its own prefered settings for security issue.
here is a work around:
register a mockup activity in Manifest.xml:
<activity
android:name="MockupLauncher"
android:enabled="false"
android:exported="false"
android:excludeFromRecents="true"
android:launchMode="singleTask" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
use "PackageManager.setComponentEnabledSetting" to trigger OS clear current prefered launcher:
private void resetPreferedLauncher() {
PackageManager pm = mContext.getPackageManager();
ComponentName mockupComponent = new ComponentName(MockupLauncher.class.getPackage().getName(), MockupLauncher.class.getName());
pm.setComponentEnabledSetting(mockupComponent, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(startMain);
pm.setComponentEnabledSetting(mockupComponent, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP);
// or
//pm.setComponentEnabledSetting(mockupComponent, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}
that is how I make it! enjoy!

Android. Prevent creating shortcut for application

I have an appilcation with an intent filter for main activity
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
because I need an "Open" button in Play Market. But I don't want any shortcuts being created on homescreen or in application list.
How can I prevent creation of shortcuts, saving my "Open" button?
If you have LAUNCHER intent filter you have an Icon, there is no way around this. Keep the icon, no user will ever figure out that he has to navigate to the Playstore and then press open there. You can let the user hide the icon, there's an API for this.
PackageManager p = getPackageManager();
p.setComponentEnabledSetting(getComponentName(),PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
This might require a reboot

Hide an application from Android application list

Is there any way to hide an application icon from Android applications list ? The application should be downloaded from Market and opened some GUI for configuring my application. I don't want to see any icon of my application in applications list. User should not be able to run it.
By the way I know some way:
remove this line from manifest category android:name="android.intent.category.LAUNCHER"
But it is not worked for me, because the GUI is not shown.
Thanks very much !
Removing the launcher category is correct.
Try adding android.intent.category.DEFAULT to the intent filter to be able to call the activity.
Thanks for replay.
I found a way to hide application icon from application list;
PackageManager pm = getApplicationContext().getPackageManager();
pm.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
But in time of application reinstalling occurs error "Activity class does not exist" and it is not possible to reinstall application without uninstalling it. .... what's the problem ?
There is also variant of Launcher Pro. But this is an application. My app should be downloaded from Android Market and there is no guarantee that all users have Launcher Pro.
I want programmatically hide application icon from application list. The method with PackageManager works for me ... but there is a problem regarding reinstalling. It is important when you want to update the application from Market.
I have found a way for this to work when you reinstall the app.
Add a broadcast receiver with intent filter action android.intent.action.PACKAGE_ADDED.
In the onReceived method you must activate your disabled component :
ComponentName componentToEnable = new ComponentName(context, Your_disabled_class.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(componentToEnable, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
Complete AndroidManifest.xml for receiver:
<receiver android:name="PackageChangeReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>

Categories

Resources