Android hide/unhide app icon programmatically - android

i had used below code for hide app icon programmatically
try{
PackageManager p = getPackageManager();
p.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}catch (Exception e) {
e.printStackTrace();
}
Now I want to make icon visible programmatically

Hide app's icon using below code:
PackageManager p = getPackageManager();
ComponentName componentName = new ComponentName(this, com.apps.MainActivity.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);
Here is how to bring back the app's icon.
PackageManager p = getPackageManager();
ComponentName componentName = new ComponentName(this, com.apps.MainActivity.class);
p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
Important Edit:
According to docs, as of Android Q (API 29) all app icons will be visible in the launcher no matter what unless:
As of Android Q, at least one of the app's activities or synthesized
activities appears in the returned list unless the app satisfies at
least one of the following conditions:
The app is a system app.
The app doesn't request any permissions.
The tag in the app's manifest doesn't contain any child elements that represent app components.
Additionally, the system hides synthesized activities for some or all
apps in the following enterprise-related cases:
If the device is a fully managed device, no synthesized activities for any app appear in the returned list.
If the current user has a work profile, no synthesized activities for the user's work apps appear in the returned list.

Best Way To Hide Application Icon From Launcher You Can Use
<category android:name="android.intent.category.LEANBACK_LAUNCHER"/>
In Your Manifest MainActivity
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER"/>
</intent-filter>
</activity>
also add uses-feature in Manifest Tag
<uses-feature
android:name="android.software.leanback"
android:required="true" />

To hide icon use this:
PackageManager p = getPackageManager();
ComponentName componentName = new ComponentName(this, com.apps.MainActivity.class);
p.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
and to unhide icon:
PackageManager p = getPackageManager();
ComponentName componentName = new ComponentName(this, com.apps.MainActivity.class);
p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
IMPORTANT:
It's somehow tricky if you need to do something with main activity in your app when it's hidden. you will face an ActivityNotFoundException. to make it work, you should unhide icon before doing anything to your main activity and hide it again after you are finished.
simple steps:
1-call received here
2-unhide icon
3-launch main activity
4-do your things on main activity
5-hide icon again

Download source code from here (Hide and Unhide the app icon in android programmatically)
MainActivity.java:
package com.deepshikha.hideappicon;
import android.Manifest;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btn_hide;
private static final ComponentName LAUNCHER_COMPONENT_NAME = new ComponentName(
"com.deepshikha.hideappicon", "com.deepshikha.hideappicon.Launcher");
public static int REQUEST_PERMISSIONS = 1;
boolean boolean_permission;
ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
fn_permission();
listener();
}
private void init() {
btn_hide = (Button) findViewById(R.id.btn_hide);
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Alert");
progressDialog.setMessage("Please wait");
if (isLauncherIconVisible()) {
btn_hide.setText("Hide");
} else {
btn_hide.setText("Unhide");
}
}
private void listener() {
btn_hide.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_hide:
progressDialog.show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
progressDialog.dismiss();
if (isLauncherIconVisible()) {
btn_hide.setText("Hide");
} else {
btn_hide.setText("Unhide");
}
}
}, 10000);
if (boolean_permission) {
if (isLauncherIconVisible()) {
fn_hideicon();
} else {
fn_unhide();
}
} else {
Toast.makeText(getApplicationContext(), "Please allow the permission", Toast.LENGTH_LONG).show();
}
break;
}
}
private boolean isLauncherIconVisible() {
int enabledSetting = getPackageManager().getComponentEnabledSetting(LAUNCHER_COMPONENT_NAME);
return enabledSetting != PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
}
private void fn_hideicon() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Important!");
builder.setMessage("To launch the app again, dial phone number 1234567890");
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
getPackageManager().setComponentEnabledSetting(LAUNCHER_COMPONENT_NAME,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
});
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.show();
}
private void fn_unhide() {
PackageManager p = getPackageManager();
ComponentName componentName = new ComponentName(this, com.deepshikha.hideappicon.MainActivity.class);
p.setComponentEnabledSetting(LAUNCHER_COMPONENT_NAME, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}
private void fn_permission() {
if ((ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.PROCESS_OUTGOING_CALLS) != PackageManager.PERMISSION_GRANTED) ||
(ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.PROCESS_OUTGOING_CALLS) != PackageManager.PERMISSION_GRANTED)) {
if ((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.PROCESS_OUTGOING_CALLS))) {
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.PROCESS_OUTGOING_CALLS},
REQUEST_PERMISSIONS);
}
if ((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.PROCESS_OUTGOING_CALLS))) {
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.PROCESS_OUTGOING_CALLS},
REQUEST_PERMISSIONS);
}
} else {
boolean_permission = true;
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSIONS) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
boolean_permission = true;
} else {
Toast.makeText(getApplicationContext(), "Please allow the permission", Toast.LENGTH_LONG).show();
}
}
}
}
LaunchAppReceiver.java:
package com.deepshikha.hideappicon;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
/**
* Created by deepshikha on 9/6/17.
*/
public class LaunchAppReceiver extends BroadcastReceiver {
String LAUNCHER_NUMBER = "1234567890";
private static final ComponentName LAUNCHER_COMPONENT_NAME = new ComponentName(
"com.deepshikha.hideappicon", "com.deepshikha.hideappicon.Launcher");
#Override
public void onReceive(Context context, Intent intent) {
String phoneNubmer = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
if (LAUNCHER_NUMBER.equals(phoneNubmer)) {
setResultData(null);
if (isLauncherIconVisible(context)) {
} else {
Intent appIntent = new Intent(context, MainActivity.class);
appIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(appIntent);
}
}
}
private boolean isLauncherIconVisible(Context context) {
int enabledSetting = context.getPackageManager().getComponentEnabledSetting(LAUNCHER_COMPONENT_NAME);
return enabledSetting != PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
}
}
Thanks!

this is what I've found so far, unfortunately it's not an answer to the original question, just alternatives
This is the first option, but if your apps require permission and is not useful anymore (at least in Android 10) as #CoronaPintu mentioned here https://stackoverflow.com/a/22754642/1712446 this method works but have many restrictions
private void hideIcon(Context context, Class activityToHide) {
PackageManager packageManager = getPackageManager();
ComponentName componentName = new ComponentName(context, activityToHide);
packageManager.setComponentEnabledSetting(
componentName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
Using the same method above plus adb command, even is your app require permission this alternative works, but you must have access to devices and connect to a pc, then run this command
to hide:
$adb shell settings put global show_hidden_icon_apps_enabled 0
to show:
$adb shell settings put global show_hidden_icon_apps_enabled 1
Just in case, you cannot run this command from the app
Another option is DevicePolicyManager
private void hideIcon(Context context, Class activityToHide) {
ComponentName componentName = new ComponentName(context, activityToHide);
DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(getApplicationContext().DEVICE_POLICY_SERVICE);
devicePolicyManager.setApplicationHidden(componentName, "your.package.name.here", true);
}
This method works, but again we have some restrictions, you need to
enable Device Owner Mode, you can find more info here
To enable this mode you must run this adb command
adb shell dpm set-device-owner my.package.name/.DevAdminReceiver
However you can this command from the app
Runtime.getRuntime().exec("dpm set-device-owner my.package.name/.DevAdminReceiver");
But, if the phone already have set an account, this method going to failed with the next error:
java.lang.IllegalStateException: Not allowed to set the device owner because there are already several users on the device

This feature is no longer supported as of Android Q (API 29). Details have also been added to a previous answer. Your app's icon will be visible unless it satisfies one of the following conditions stated in the docs:
The app is a system app.
The app doesn't request any permissions.
The tag in the app's manifest doesn't contain any child elements that
represent app components.

Related

How to reset the password of the lock screen programmatically on Android SDK 26 or higher

In my application I want to change the password of the lock screen programmatically. So I wrote this method to reset the password:
#TargetApi(26)
private void changePasswordWithToken() {
SecureRandom secureRandom = new SecureRandom();
byte[] token = secureRandom.generateSeed(32);
DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getApplicationContext().getSystemService(
DEVICE_POLICY_SERVICE);
if (devicePolicyManager != null) {
devicePolicyManager.setResetPasswordToken(compName, token);
devicePolicyManager.resetPasswordWithToken(compName, "1234", token, 0);
}
}
When I call the method I get this error on my device running Android 9 SDK 27
va.lang.SecurityException: Admin ComponentInfo{com.xxx.xxx/com.xxx.xxxx.MyAdmin} does not own the profile
at android.os.Parcel.createException(Parcel.java:1942)
at android.os.Parcel.readException(Parcel.java:1910)
at android.os.Parcel.readException(Parcel.java:1860)
at android.app.admin.IDevicePolicyManager$Stub$Proxy.setResetPasswordToken(IDevicePolicyManager.java:9995)
at android.app.admin.DevicePolicyManager.setResetPasswordToken(DevicePolicyManager.java:3091)
at com.ssaurel.lockdevice.MainActivity.changePasswordWithToken(MainActivity.java:136)
at com.xx.xx.MainActivity.onClick(MainActivity.java:93)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
...
Before I call this method, I' getting the device admin permissions with this method
private void provisionDeviceAdmin() {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(EXTRA_DEVICE_ADMIN, compName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"Additional text explaining why we need this permission");
startActivityForResult(intent, RESULT_ENABLE);
}
My policies looks like this
<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<force-lock />
<reset-password />
</uses-policies>
</device-admin>
Please have a look at the documentation, it clearly states there that:
Called by device or profile owner to force set a new device unlock
password or a managed profile challenge on current user. This takes
effect immediately.
From your code it does not look as if you are a "device or profile owner"; please don't mix that up with "device admin" which your apps seems to be (or is trying to get not sure if it is really successful from your code).
For me this solution worked out:
At first, define admin device permissions
policies.xml
<?xml version="1.0" encoding="utf-8"?>
<device-admin>
<uses-policies>
<reset-password/>
</uses-policies>
</device-admin>
Then create a class that extend DeviceAdminReceiver
public class MyAdmin extends DeviceAdminReceiver {
#Override
public void onEnabled(Context context, Intent intent) {
Toast.makeText(context, "Device Admin : enabled", Toast.LENGTH_SHORT).show();
}
#Override
public void onDisabled(Context context, Intent intent) {
Toast.makeText(context, "Device Admin : disabled", Toast.LENGTH_SHORT).show();
}
/**
* Generates a {#link ComponentName} that is used throughout the app.
* #return a {#link ComponentName}
*/
public static ComponentName getComponentName(Context context) {
return new ComponentName(context.getApplicationContext(), MyAdmin.class);
}
}
Get admin device permission with this function in your MainActivity
private void provisionDeviceAdmin() {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(EXTRA_DEVICE_ADMIN, MyAdmin.getComponentName(this));
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"Additional text explaining why we need this permission");
startActivityForResult(intent, RESULT_ENABLE);
}
Then provision a working profile for your app in the MainActivity
private void provisionManagedProfile() {
Intent intent = new Intent(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE);
// Use a different intent extra below M to configure the admin component.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
//noinspection deprecation
intent.putExtra(DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME,
MyAdmin.getComponentName(this));
} else {
final ComponentName component = new ComponentName(this,
MyAdmin.class.getName());
intent.putExtra(DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME,
component);
}
if (intent.resolveActivity(this.getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_PROVISION_MANAGED_PROFILE);
this.finish();
} else {
Toast.makeText(this, "Device provisioning is not enabled. Stopping.",
Toast.LENGTH_SHORT).show();
}
}
After that set the application package to the working profile
private void setAppEnabled(String packageName, boolean enabled) {
PackageManager packageManager = getPackageManager();
DevicePolicyManager devicePolicyManager =
(DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
try {
int packageFlags;
if(Build.VERSION.SDK_INT < 24){
//noinspection deprecation
packageFlags = PackageManager.GET_UNINSTALLED_PACKAGES;
}else{
packageFlags = PackageManager.MATCH_UNINSTALLED_PACKAGES;
}
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName,
packageFlags);
// Here, we check the ApplicationInfo of the target app, and see if the flags have
// ApplicationInfo.FLAG_INSTALLED turned on using bitwise operation.
if (0 == (applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED)) {
// If the app is not installed in this profile, we can enable it by
// DPM.enableSystemApp
if (enabled) {
devicePolicyManager.enableSystemApp(MyAdmin.getComponentName(this), packageName);
} else {
// But we cannot disable the app since it is already disabled
Log.e("TAG", "Cannot disable this app: " + packageName);
return;
}
} else {
// If the app is already installed, we can enable or disable it by
// DPM.setApplicationHidden
devicePolicyManager.setApplicationHidden(
MyAdmin.getComponentName(this), packageName, !enabled);
}
Toast.makeText(this, enabled ? "Enabled" : "Disabled",
Toast.LENGTH_SHORT).show();
} catch (PackageManager.NameNotFoundException e) {
Log.e("TAG", "The app cannot be found: " + packageName, e);
}
}
Create a method to generate random password tokens
private byte[] generateRandomPasswordToken() {
try {
return SecureRandom.getInstance("SHA1PRNG").generateSeed(32);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
Finally implement this method to reset the lock screen password with token
#TargetApi(26)
private void changePasswordWithToken() {
byte[] token = generateRandomPasswordToken();
DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getApplicationContext().getSystemService(
DEVICE_POLICY_SERVICE);
KeyguardManager keyguardManager = (KeyguardManager) this.getSystemService(KEYGUARD_SERVICE);
keyguardManager.createConfirmDeviceCredentialIntent(null, null);
if (devicePolicyManager != null) {
devicePolicyManager.setResetPasswordToken(MyAdmin.getComponentName(this), token);
devicePolicyManager.resetPasswordWithToken(MyAdmin.getComponentName(this), "1234", token, 0);
}
}

Android M startActivity battery optimization

I'm developing an app that should alert an user if is near a place.
and of course have to do that also if the phone is in idle.
With DOZE now I understood that I have to whitelist my app, and to do that I saw that I can start an intent with the action request thanks to Buddy's answer in this post
Intent intent = new Intent();
String packageName = context.getPackageName();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (pm.isIgnoringBatteryOptimizations(packageName))
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
else {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
}
context.startActivity(intent);
well this should be too easy...because google doesn't like this approach and if you do it, your app should be banned from the play store...no comment...
Ok so the way should be to drive the user to the battery settings and manually add your app in the DOZE's white list...yes this should be a big wall to climb...anyway seems to be the only way...now the answer is:
I Can use an intent to go to the power usage summary, in this way (thank you Chris):
Intent powerUsageIntent = new Intent(Intent.ACTION_POWER_USAGE_SUMMARY);
ResolveInfo resolveInfo = getPackageManager().resolveActivity(powerUsageIntent, 0);
// check that the Battery app exists on this device
if(resolveInfo != null){
startActivity(powerUsageIntent);
}
But how to directly go at the list of app for choosing the battery optimization?
Thanks for any answer.
To open list of apps for choosing battery optimization you can use this code sample:
private void openPowerSettings(Context context) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
context.startActivity(intent);
}
It doesn't need to have <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/> permission, so it should be ok to publish it to Google Play (for details please check this thread and comments to this question).
NOTE
Adding this line
intent.setData(Uri.parse("package:" + mContext.getPackageName()));
will cause crash "Fatal Exception: android.content.ActivityNotFoundException No Activity found to handle Intent { act=android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS dat=package:io.demo.example }". User has to find the app in the list, it seems no way to jump directly to our app.
This is what I use:
manifest:
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.PowerManager
import android.provider.Settings
import androidx.annotation.RequiresPermission
import androidx.core.content.ContextCompat
object PowerSaverHelper {
enum class WhiteListedInBatteryOptimizations {
WHITE_LISTED, NOT_WHITE_LISTED, ERROR_GETTING_STATE, IRRELEVANT_OLD_ANDROID_API
}
fun getIfAppIsWhiteListedFromBatteryOptimizations(context: Context, packageName: String = context.packageName): WhiteListedInBatteryOptimizations {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return WhiteListedInBatteryOptimizations.IRRELEVANT_OLD_ANDROID_API
val pm = context.getSystemService(Context.POWER_SERVICE) as PowerManager?
?: return WhiteListedInBatteryOptimizations.ERROR_GETTING_STATE
return if (pm.isIgnoringBatteryOptimizations(packageName)) WhiteListedInBatteryOptimizations.WHITE_LISTED else WhiteListedInBatteryOptimizations.NOT_WHITE_LISTED
}
//#TargetApi(VERSION_CODES.M)
#SuppressLint("BatteryLife", "InlinedApi")
#RequiresPermission(Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
fun prepareIntentForWhiteListingOfBatteryOptimization(context: Context, packageName: String = context.packageName, alsoWhenWhiteListed: Boolean = false): Intent? {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
return null
if (ContextCompat.checkSelfPermission(context, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) == PackageManager.PERMISSION_DENIED)
return null
val appIsWhiteListedFromPowerSave: WhiteListedInBatteryOptimizations = getIfAppIsWhiteListedFromBatteryOptimizations(context, packageName)
var intent: Intent? = null
when (appIsWhiteListedFromPowerSave) {
WhiteListedInBatteryOptimizations.WHITE_LISTED -> if (alsoWhenWhiteListed) intent = Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
WhiteListedInBatteryOptimizations.NOT_WHITE_LISTED -> intent = Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).setData(Uri.parse("package:$packageName"))
WhiteListedInBatteryOptimizations.ERROR_GETTING_STATE, WhiteListedInBatteryOptimizations.IRRELEVANT_OLD_ANDROID_API -> {
}
}
return intent
}
}
Example:
PowerSaverHelper.prepareIntentForWhiteListingOfBatteryOptimization(this)?.let { startActivity(it) }
Try the below code to open Ignore Battery Optimization Settings page.
private void openPowerSettings() {
startActivityForResult(new Intent(android.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS), 0);
}
No extra permissions are required to be added to the manifest file.

Launching another activity from a 'screen pinned' activity

http://florent-dupont.blogspot.ro/2015/02/android-5-screen-pinning.html
From a pinned app, you cannot start a secondary app, unless this one
has the same shared user ID (which means that the sharedUserIdis set
in the AndroidManifest.xml and that second application is packaged
with the same certificate). Other apps’ Activities won’t be allowed to
be started and doing so (by using Context.startActivity()) will simply
be ignored.
I have done just those two things above but startActivity() is still getting ignored.
From https://developer.android.com/reference/android/R.attr.html#lockTaskMode:
If the system is already in lockTask mode when a new task rooted at
this activity is launched that task will or will not start depending
on whether the package of this activity has been whitelisted.
It looks tome like I have taken the necessary steps for this to work.
Anybody out there who got this working ?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
ComponentName deviceAdmin = new ComponentName(this, AdminReceiver.class);
mDpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
if (!mDpm.isAdminActive(deviceAdmin)) {
Toast.makeText(this, getString(R.string.not_device_admin), Toast.LENGTH_SHORT).show();
}
if (mDpm.isDeviceOwnerApp(getPackageName()))
{
mDpm.setLockTaskPackages(deviceAdmin, new String[]{getPackageName(), "com.that.other.package"});
try
{
enableKioskMode(true);
PackageManager pm = this.getPackageManager();
Intent it = pm.getLaunchIntentForPackage("com.that.other.package");
if (null != it) {
this.startActivity(it);
Log.d(_TAG, "Started activity for com.that.other.package");
}
}
catch (Exception e)
{
Log.e(_TAG, e.getMessage());
finish();
}
} else {
Toast.makeText(this, getString(R.string.not_device_owner), Toast.LENGTH_SHORT).show();
}
}
private void enableKioskMode(boolean enabled) throws Exception
{
if (enabled)
{
if (mDpm.isLockTaskPermitted(this.getPackageName()))
{
startLockTask();
mIsKioskEnabled = true;
mButton.setText(getString(R.string.exit_kiosk_mode));
} else {
Toast.makeText(this, getString(R.string.kiosk_not_permitted), Toast.LENGTH_SHORT).show();
}
} else {
stopLockTask();
mIsKioskEnabled = false;
mButton.setText(getString(R.string.enter_kiosk_mode));
}
}
Turned out I was missing the android:taskAffinity tag. Point it to the same strings in both packages and startActivity() should start behaving.

Hiding Application Icon programmatically [duplicate]

i had used below code for hide app icon programmatically
try{
PackageManager p = getPackageManager();
p.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}catch (Exception e) {
e.printStackTrace();
}
Now I want to make icon visible programmatically
Hide app's icon using below code:
PackageManager p = getPackageManager();
ComponentName componentName = new ComponentName(this, com.apps.MainActivity.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);
Here is how to bring back the app's icon.
PackageManager p = getPackageManager();
ComponentName componentName = new ComponentName(this, com.apps.MainActivity.class);
p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
Important Edit:
According to docs, as of Android Q (API 29) all app icons will be visible in the launcher no matter what unless:
As of Android Q, at least one of the app's activities or synthesized
activities appears in the returned list unless the app satisfies at
least one of the following conditions:
The app is a system app.
The app doesn't request any permissions.
The tag in the app's manifest doesn't contain any child elements that represent app components.
Additionally, the system hides synthesized activities for some or all
apps in the following enterprise-related cases:
If the device is a fully managed device, no synthesized activities for any app appear in the returned list.
If the current user has a work profile, no synthesized activities for the user's work apps appear in the returned list.
Best Way To Hide Application Icon From Launcher You Can Use
<category android:name="android.intent.category.LEANBACK_LAUNCHER"/>
In Your Manifest MainActivity
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER"/>
</intent-filter>
</activity>
also add uses-feature in Manifest Tag
<uses-feature
android:name="android.software.leanback"
android:required="true" />
To hide icon use this:
PackageManager p = getPackageManager();
ComponentName componentName = new ComponentName(this, com.apps.MainActivity.class);
p.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
and to unhide icon:
PackageManager p = getPackageManager();
ComponentName componentName = new ComponentName(this, com.apps.MainActivity.class);
p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
IMPORTANT:
It's somehow tricky if you need to do something with main activity in your app when it's hidden. you will face an ActivityNotFoundException. to make it work, you should unhide icon before doing anything to your main activity and hide it again after you are finished.
simple steps:
1-call received here
2-unhide icon
3-launch main activity
4-do your things on main activity
5-hide icon again
Download source code from here (Hide and Unhide the app icon in android programmatically)
MainActivity.java:
package com.deepshikha.hideappicon;
import android.Manifest;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btn_hide;
private static final ComponentName LAUNCHER_COMPONENT_NAME = new ComponentName(
"com.deepshikha.hideappicon", "com.deepshikha.hideappicon.Launcher");
public static int REQUEST_PERMISSIONS = 1;
boolean boolean_permission;
ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
fn_permission();
listener();
}
private void init() {
btn_hide = (Button) findViewById(R.id.btn_hide);
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Alert");
progressDialog.setMessage("Please wait");
if (isLauncherIconVisible()) {
btn_hide.setText("Hide");
} else {
btn_hide.setText("Unhide");
}
}
private void listener() {
btn_hide.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_hide:
progressDialog.show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
progressDialog.dismiss();
if (isLauncherIconVisible()) {
btn_hide.setText("Hide");
} else {
btn_hide.setText("Unhide");
}
}
}, 10000);
if (boolean_permission) {
if (isLauncherIconVisible()) {
fn_hideicon();
} else {
fn_unhide();
}
} else {
Toast.makeText(getApplicationContext(), "Please allow the permission", Toast.LENGTH_LONG).show();
}
break;
}
}
private boolean isLauncherIconVisible() {
int enabledSetting = getPackageManager().getComponentEnabledSetting(LAUNCHER_COMPONENT_NAME);
return enabledSetting != PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
}
private void fn_hideicon() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Important!");
builder.setMessage("To launch the app again, dial phone number 1234567890");
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
getPackageManager().setComponentEnabledSetting(LAUNCHER_COMPONENT_NAME,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
});
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.show();
}
private void fn_unhide() {
PackageManager p = getPackageManager();
ComponentName componentName = new ComponentName(this, com.deepshikha.hideappicon.MainActivity.class);
p.setComponentEnabledSetting(LAUNCHER_COMPONENT_NAME, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}
private void fn_permission() {
if ((ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.PROCESS_OUTGOING_CALLS) != PackageManager.PERMISSION_GRANTED) ||
(ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.PROCESS_OUTGOING_CALLS) != PackageManager.PERMISSION_GRANTED)) {
if ((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.PROCESS_OUTGOING_CALLS))) {
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.PROCESS_OUTGOING_CALLS},
REQUEST_PERMISSIONS);
}
if ((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.PROCESS_OUTGOING_CALLS))) {
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.PROCESS_OUTGOING_CALLS},
REQUEST_PERMISSIONS);
}
} else {
boolean_permission = true;
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSIONS) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
boolean_permission = true;
} else {
Toast.makeText(getApplicationContext(), "Please allow the permission", Toast.LENGTH_LONG).show();
}
}
}
}
LaunchAppReceiver.java:
package com.deepshikha.hideappicon;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
/**
* Created by deepshikha on 9/6/17.
*/
public class LaunchAppReceiver extends BroadcastReceiver {
String LAUNCHER_NUMBER = "1234567890";
private static final ComponentName LAUNCHER_COMPONENT_NAME = new ComponentName(
"com.deepshikha.hideappicon", "com.deepshikha.hideappicon.Launcher");
#Override
public void onReceive(Context context, Intent intent) {
String phoneNubmer = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
if (LAUNCHER_NUMBER.equals(phoneNubmer)) {
setResultData(null);
if (isLauncherIconVisible(context)) {
} else {
Intent appIntent = new Intent(context, MainActivity.class);
appIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(appIntent);
}
}
}
private boolean isLauncherIconVisible(Context context) {
int enabledSetting = context.getPackageManager().getComponentEnabledSetting(LAUNCHER_COMPONENT_NAME);
return enabledSetting != PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
}
}
Thanks!
this is what I've found so far, unfortunately it's not an answer to the original question, just alternatives
This is the first option, but if your apps require permission and is not useful anymore (at least in Android 10) as #CoronaPintu mentioned here https://stackoverflow.com/a/22754642/1712446 this method works but have many restrictions
private void hideIcon(Context context, Class activityToHide) {
PackageManager packageManager = getPackageManager();
ComponentName componentName = new ComponentName(context, activityToHide);
packageManager.setComponentEnabledSetting(
componentName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
Using the same method above plus adb command, even is your app require permission this alternative works, but you must have access to devices and connect to a pc, then run this command
to hide:
$adb shell settings put global show_hidden_icon_apps_enabled 0
to show:
$adb shell settings put global show_hidden_icon_apps_enabled 1
Just in case, you cannot run this command from the app
Another option is DevicePolicyManager
private void hideIcon(Context context, Class activityToHide) {
ComponentName componentName = new ComponentName(context, activityToHide);
DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(getApplicationContext().DEVICE_POLICY_SERVICE);
devicePolicyManager.setApplicationHidden(componentName, "your.package.name.here", true);
}
This method works, but again we have some restrictions, you need to
enable Device Owner Mode, you can find more info here
To enable this mode you must run this adb command
adb shell dpm set-device-owner my.package.name/.DevAdminReceiver
However you can this command from the app
Runtime.getRuntime().exec("dpm set-device-owner my.package.name/.DevAdminReceiver");
But, if the phone already have set an account, this method going to failed with the next error:
java.lang.IllegalStateException: Not allowed to set the device owner because there are already several users on the device
This feature is no longer supported as of Android Q (API 29). Details have also been added to a previous answer. Your app's icon will be visible unless it satisfies one of the following conditions stated in the docs:
The app is a system app.
The app doesn't request any permissions.
The tag in the app's manifest doesn't contain any child elements that
represent app components.

changing UI elements if app is in the foreground from a broadcast receiver

I Have a BroadcastReceiver set up to turn Bluetooth on and off according to power state (when plugged in, bluetooth is on, unplugged, bluetooth is off). This is working just fine (yay!). however, my very simple app has a single button, which also turns Bluetooth on and off, and has the text "Bluetooth on" or "Bluetooth Off", as applicable. I would like to update this single button, BUT, I only have to update it if the app is in the foreground.
Inside onResume on m,y main activity, I'm calling my updateUI method, which checks the Bluetooth state, and updates the button accordingly. however, that only applies if the program was open and in the background, and is resumed, NOT if i'm in the program while plugging/unplugging the power.
I created a new activity (CheckIfAppIsRunningActivity.java) with this code which is supposed to check if my app is running in the foreground, and if so, take it to the activity (BluetoothOnActivity) which will update the button:
package vermel.BluetoothOn;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class CheckIfAppIsRunningActivity extends Activity{
public void onCreate() {
checkStatus();
}
private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Intent it = new Intent();
it.setClassName("vermel.BluetoothOn", "vermel.BluetoothOn.BluetoothOnActivity");
context.startActivity(it);
}
};
public void checkStatus() {
ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runningProcInfo = activityManager.getRunningAppProcesses();
for(int i = 0; i < runningProcInfo.size(); i++){
if(runningProcInfo.get(i).processName.equals("vermel.BluetoothOn")) {
if (runningProcInfo.get(i).lru == RunningAppProcessInfo.IMPORTANCE_FOREGROUND){
//start activity
/* Intent it = new Intent();
it.setClassName("vermel.BluetoothOn", "vermel.BluetoothOn.BluetoothOnActivity");
context.startActivity(it); */
}
}
}
}
}
and i'm pointing to it from my broadcast receiver:
package vermel.BluetoothOn;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.widget.Toast;
public class BTDetector extends BroadcastReceiver {
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
public void onReceive(Context context , Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_POWER_CONNECTED)) {
if (!mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.enable();
//TODO if app is open, change text on button to on
//Toast.makeText(context, "turned on bluetooth", Toast.LENGTH_LONG).show();
Intent i = new Intent();
i.setClassName("vermel.BluetoothOn", "vermel.BluetoothOn.CheckIfAppIsRunningActivity");
context.startActivity(i);
}
} else if (action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
if (mBluetoothAdapter.isEnabled()){
mBluetoothAdapter.disable();
//TODO if app is open, change text on button to off
//Toast.makeText(context, "turned off bluetooth", Toast.LENGTH_LONG).show();
Intent i = new Intent();
i.setClassName("vermel.BluetoothOn", "vermel.BluetoothOn.CheckIfAppIsRunningActivity");
context.startActivity(i);
}
}
}
}
a few things: yes, I know i'm not supposed to use .enable() without user permission. in a weird way, plugging in the phone IS my user permission, since this is ALL that this app does, so, it's not sneaky, since you know what you're getting when you're installing the app.
The commented stuff is mostly things i've tried in vain..
I'm very open to the fact that i'm making this WAY harder than I need to...
so, as i said, it does turn Bluetooth on and off beautifully, but simply crashes after that. I can't debug it, since the emulator doesn't have Bluetooth , and i'm disconnecting the phone to get the crash result, so, it's not logging anything, since it's now connected...
I'm new in both Java and Android, and would appreciate a bit of patience. I try reading the official android documentation, but that's like chinese to me... so, an extended explanation would be great...
Thanks for reading!

Categories

Resources