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);
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 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!
I'm trying to build an android app which needs to disable a home key in android version 4+ ,
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
My problem is when i want to return to android launcher "default home screen" using this code :
PackageManager p = getPackageManager();
ComponentName cN = new ComponentName(MainActivity.this, MainActivity.class);
p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
the default home screen lunched but the application is killed .
How I can lunch the home screen without killing the application.
I tried it just now and its working for me. My application is not killed at any point. I am using android 4.1
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
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>
I want to remove my app from list of apps and the recent apps list. So I tried to disable my main / launcher activity with the following code:
ComponentName componentToDisable = new ComponentName(context, MainActivity.class);
context.getPackageManager().setComponentEnabledSetting(componentToDisable,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
This does the job. But now I try to reinstall the app and it fails saying "activity MainActivity class doesn't exist". If I uninstall the app, the installation works again. How can I handle this issue? Thanks a lot for your time and help
I found that I have to make the activity enabled before reinstalling it.
This can be done by having a broadcast receiver listen to package_add / remove events and in onReceive make the activity enabled again.
public void onReceive(Context context, Intent intent) {
Log.i("Receiver","got event");
ComponentName componentToDisable = new ComponentName(context,BlockableComponentActivity.class);
context.getPackageManager().setComponentEnabledSetting(componentToDisable,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}
Manifest excerpt for the 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>