set custom launcher as default launcher by code - android

I am able to reset the default launcher by code with the help of following link
How to reset default launcher/home screen replacement?
Now, I want to set my custom launcher as default launcher. But the following code is not working. It crashes
ComponentName componentName = new ComponentName("com.xxx.launcher", "com.xxx.launcher.LauncheActivity");
PackageManager pm = getPackageManager();
int flag = ((pm.getComponentEnabledSetting(componentName) == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) ? PackageManager.COMPONENT_ENABLED_STATE_DISABLED
: PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
pm.setComponentEnabledSetting(componentName, flag, PackageManager.DONT_KILL_APP); //crash at this line..

I want to set my custom launcher as default launcher
That is not possible. The user can set your home screen as the default home screen, such as by pressing HOME and choosing your app as the default. However, you cannot force the user to accept your home screen as the default.
But the following code is not working. It crashes
That code does not set your "custom launcher as default launcher". That simply controls whether or not you are even a potential option for being a home screen (if you are disabled, you cannot possibly be the home screen).
As to why it is crashing, without a stack trace, that will be difficult to determine.

Related

Resetting default launcher to system factory default

I ran into the following issue: I have an app I want my user to be able to select it as the launcher app. So far, no issues there.
Clearing said selection programmatically also isnt an issue here. Where my issue occurs is the following:
When I my custom launcher programmatically I want to go back to default launcher app, e.g. the default on a samsung galaxy. clearing the launch settings over clearPackagePrefferedActivities clears all of it but once i press the home button I get the dialog to select a default launcher again.
Is there anyway to revert back programmatically to the default launcher?
The user always needs to confirm a change in the default settings. You can always trigger the "default launcher" dialog by
PackageManager pm = getPackageManager();
ComponentName cm = new ComponentName(this, FakeLauncherActivity.class);
pm.setComponentEnabledSetting(cm, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
Intent homeIntent= new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(homeIntent);
pm.setComponentEnabledSetting(cm, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
Where FakeLauncherActivity is just another Activity in your app with the intent-filter HOME/MAIN/DEFAULT (without LAUNCHER).
If you want to know the default system launcher you could query the intents answering the LAUNCHER Intent, but if the user has another Launcher installed, you will not know which one is the default system launcher.
Even if this is not a complete solution for your question, I hope it helps you in a useful direction.

How to hide an app and give it the right to be access from another app?

I hope anyone can help me or give me a clear solution,
I have an app that I don't want to be opened directly from the user, so I need to hide its icon.
and in the other hand, I want to access this application from another app (App2), so I wrote this code in the function of onClick of a certain button of App2:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage(package name of the first app );
startActivity(launchIntent);
Before hiding the App1 , everything was working perfectly No errors but after hiding it using the following code, it crashes:
PackageManager packageManager = this.getPackageManager();
ComponentName componentName = new ComponentName(this,FingerActivity.class);
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
and the following error appear in Android Studio:
Starting: Intent { act=android.intent.action.MAIN
cat=[android.intent.category.LAUNCHER]
cmp=com.neurotec.samples.multibiometric/.fingers.FingerActivity }
Error type 3 Error: Activity class
{com.neurotec.samples.multibiometric/com.neurotec.samples.multibiometric.fingers.FingerActivity}
does not exist.
any idea?
any idea?
You disabled the component. You cannot start the component from any other app.
I have an app that I don't want to be opened directly from the user, so I need to hide its icon.
The simplest way for it to not have an icon is for you to not give it an icon. Change your <intent-filter> for that activity to be something custom for you, rather than having it have the MAIN/LAUNCHER values that causes home screens to put an icon for it in the launcher.

Set application as default home

I have set my application as default home, Everything is going good but if i set Launcher as default home by my application(by mistake), then my app never ask to set my app as default and directly opens the Launcher home. I want that until my app is set as default home it ask user to set as default. Please help
Thanks in advance.
Create a Preferences with boolean condition ....
Stored value false as default ...
start the Launcher by checking this condition
if the user sets your app as default launcher make the boolen preference true ....
You can check current default launcher by this piece of code:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
ResolveInfo resolveInfo = getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
String currentHomePackage = resolveInfo.activityInfo.packageName;
as shown in this answer: How can I get the package name of the current launcher in android 2.3 and above?
So simply check if currentHomePackage equals your App's Package and act accordingly.
If the user sets another App as Default Launcher the Dialog to choose a Default Launcher would only appear again if he delets the Launchers Defaults or removes/installs an App which can act as default launcher.

How do I undo clearPackagePreferredActivities("com.android.launcher");

What I am trying to do is replicate what the ToddlerLock app does. I have managed to clear the default launcher with
PackageManager localPackageManager = getPackageManager();
localPackageManager.clearPackagePreferredActivities("com.android.launcher");
and then open the launch select dialog with this
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
this.startActivity(i);
As long as the user checks the "use by default for this action" the home key now sends the user to my app, thus essentially disabling it.
I then use "clearPackagePreferredActivities("com.my_application")" when I exit my app and the user has to choose a new default home app.
My question is how can I choose the default home application (essentially checking the "use by default for this action" check box in code for the "com.android.launcher" package. That way the user does not constantly have to see that dialog box every time they open and close my app.
I think ToddlerLock does this somehow without using clearPackagePreferredActivities
because if I look at the "clear defaults" in the application manager it is not cleared and you only have to go through the set as default dialog box one time on startup and once when you exit to set it back to the normal home screen.
Thanks for your help.
I have implemented the same functionality in different way.
Let's say you have 'LockScreenAcitivity' configured as Home Screen in Manifest.
Launch LockScreenActivity by sending Home Intent.
Android will popup a dialog, to select the default Acitivity
choose your LockScreenActivity from List as default Activity
.....
.....
While closing the Activity don't clear the prefered Activities.
Disable your LockScreenActivity alone by calling PackageManager.setComponentEnabledSetting()
After you disable your LockScreenActivity, android will rollback to previous Prefered Activity (that's your old home screen ).
Next time when you launch your app,
enable your lockscreenActivity again by calling PackageManager.setComponentEnabledSetting()
Launch LockScreenActivity by sending Home Intent.

Allow a USER to remove app icon from Launcher?

I'm trying to programatically allow the USER to decide when to remove my app (a theme which is called from another app) from the launcher.
Currently using a button:
getPackageManager().setComponentEnabledSetting(new ComponentName("com.package.name","Main"),PackageManager.COMPONENT_ENABLED_STATE_DISABLED,0);
Sorry I'm not a coder and I'm doing something wrong, the button shows but no effect even with a launcher or phone restart.
Ideas?
OK, I actually found some other similar code which worked.
I had kept intent.category.LAUNCHER and intent.action.MAIN in Main and moved the intent that calls my app from a parent app into a new class (duh).
Then I run the folowing on a button press by which the user can remove the icon from the launcher (requires launcher/phone restart):
PackageManager pm = getPackageManager(); ComponentName name = new ComponentName(this, Main.class);
pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
Why is there even a launcher icon (necessary) in the first place? Does a theme need such an icon?
Apart from that: the same question was asked and answered here: you have to restart the launcher itself to update the icon-list.

Categories

Resources