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!
Related
I am able to trigger the launcher chooser popup with below code from where i can select my launcher. But in huawei tablet this code is not working
Intent selector = new Intent(Intent.ACTION_MAIN);
selector.addCategory(Intent.CATEGORY_HOME);
selector.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(selector);
Please help me!!
Solution:
This is actually possible with a little workaround:
Create an empty Activity that acts as a launcher called FakeLauncherActivity. Add it to your manifest as a disabled component:
<activity
android:name="com.path.to.your.FakeLauncherActivity"
android:enabled="false">
<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>
Check whether your desired launcher activity is the default one..
If not, offer the user to choose the preferred launcher activity like this:
public static void resetPreferredLauncherAndOpenChooser(Context context) {
PackageManager packageManager = context.getPackageManager();
ComponentName componentName = new ComponentName(context, com.path.to.your.FakeLauncherActivity.class);
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
Intent selector = new Intent(Intent.ACTION_MAIN);
selector.addCategory(Intent.CATEGORY_HOME);
selector.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(selector);
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP);
}
This method temporarily enables FakeLauncherActivity, which leads to a change in the set of available launcher activities, which forces Android to forget its default launcher. You will see something like...
521-735/system_process I/PackageManager﹕ Result set changed, dropping preferred activity for Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 } type null
... in your log.
The method then simply opens a launcher intent where you can see all installed launchers and the buttons "Always" / "Just once".
Finally, the method disables FakeLauncherActivity again so that it doesn't display in the list.
You could repeat that as often as you want and only let the user proceed if your desired launcher activity is set as default.
Source: Got It From Here
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.
I have an app which is kept hidden from the launcher. Now I want to open the app from the dialer. So I have a BroadcastReceiver where I am handling things.
1) First I show the app,
ComponentName componentName = new ComponentName(context,
SplashActivity.class);
context.getPackageManager().setComponentEnabledSetting(
componentName,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
2) Then launch an Intent,
Intent launcher = new Intent(context, SplashActivity.class);
launcher.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(launcher);
3) Then I am hiding the app again,
ComponentName componentName2 = new ComponentName(context,
SplashActivity.class);
context.getPackageManager().setComponentEnabledSetting(
componentName2,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
The Problem
The Intent launch is successful but the app is getting killed after a few seconds. But the problem doesn't occur if I launch any other activity other than the SplashActivity. What is the problem and how can it be solved?
You can use <activity-alias> tag in Android manifest for launcher activity. change in your manifest for launcher activity like as below:-
<activity-alias
android:name="com.watever.SplashActivityAlias"
android:targetActivity="com.watever.SplashActivity"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<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.MONKEY" />
</intent-filter>
</activity-alias>
and in code where where you are showing and hiding app just use Alias Name which is used in manifest like in above manifest alias name is SplashActivityAlias so use SplashActivityAlias instead of SplashActivity in both conditions where you are showing and hiding app.
By doing above thing you can able to hide your app icon with
setComponent EnabledSetting method using Alias name and you can launch your main activity which is splash activity by using dialer
For hiding app
ComponentName componentName2 = new ComponentName("com.packagename",
"com.packagename.HideAppActivity");
context.getPackageManager().setComponentEnabledSetting(
componentName2,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
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"
I have 2 questions about launcher below:
I have 3-4 launchers on device, how can i know which launcher has been set default on my device (with code).(Done)
I have own custom launcher app, I want clear default launcher on my app and without use:
Intent uninstallIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(uninstallIntent);
as some app as: Kid's Shell or Kids Place. I have try follow Clearing and setting the default home application but nothing change.
Please show me how to solve 2 things.
Thanks for any advise.
getPackageManager().clearPackagePreferredActivities(defaultLauncherPackgeName);
Added:
If you want to set your launcher as default, try:
ComponentName cN = new ComponentName(mContext, FakeHome.class);
p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
Intent selector = new Intent(Intent.ACTION_MAIN);
selector.addCategory(Intent.CATEGORY_HOME);
mContext.startActivity(selector);
p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
And in AndroidManifest.xml:
<activity
android:name="com.test.FakeHome"
android:enabled="false" >
<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>