It's very important to make product retention, and it's very important to make small white. How to keep your app active all the time? Here's another way to create a shortcut like system icon in the mobile phone's breath screen. Then hide the icon of your application. Now let's look at the concrete steps:
1. How to monitor the mobile phone's screen.
General product integration tool classes have similar approaches.
public class ScreenStatusChangeReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (BuildConfig.DEBUG) LogUtil.d("ScreenStatusChangeReceiver", "onreceive"); String action = intent.getAction(); if(Intent.ACTION_SCREEN_ON.equals(action)){ if (BuildConfig.DEBUG) { LogUtil.d("ScreenStatusChangeReceiver", "OnScreenOn"); } EventBus.getDefault().post(new OnScreenOn()); } else if(Intent.ACTION_SCREEN_OFF.equals(action)){ if(BuildConfig.DEBUG) { LogUtil.d("ScreenStatusChangeReceiver", "OnScreenOff"); } EventBus.getDefault().post(new OnScreenOff()); } else if (Intent.ACTION_USER_PRESENT.equals(action)) { if(BuildConfig.DEBUG) { LogUtil.d("ScreenStatusChangeReceiver", "OnUserPresent"); } EventBus.getDefault().post(new OnUserPresent()); } } }
Register the broadcast dynamically at application startup:
ScreenStatusChangeReceiver mScreenReceiver = new ScreenStatusChangeReceiver(); IntentFilter screenStateFilter = new IntentFilter(); screenStateFilter.setPriority(90000); screenStateFilter.addAction(Intent.ACTION_SCREEN_ON); screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF); screenStateFilter.addAction(Intent.ACTION_USER_PRESENT); ApplicationEx.getInstance().registerReceiver(mScreenReceiver, screenStateFilter);
EventBus finally sends the event out.
2. How to create shortcuts
Because many domestic ROM manufacturers have different customizations and versions of the api, here I encapsulated the tool class for your reference only, which contains the processing of 8.0 shortcuts, the processing of Build.MODEL for SM-G9508, and the judgment of whether the shortcuts are created successfully. Welcome to download.
3. How to hide desktop icons? How to recover? How to judge whether the desktop hidden icon is successful?
Hide desktop icons:
public static void magicHide() { try { Context context = ApplicationEx.getInstance(); PackageManager pm = context.getPackageManager(); ComponentName cn = new ComponentName(context, SplashActivity.class); pm.setComponentEnabledSetting(cn, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); } catch (Exception e) { LogUtil.error(e); } }
Restore desktop icons:
public static void magicShow() { try { Context context = ApplicationEx.getInstance(); PackageManager pm = context.getPackageManager(); ComponentName cn = new ComponentName(context, SplashActivity.class); pm.setComponentEnabledSetting(cn, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); } catch (Exception e) { LogUtil.error(e); } }
Determine whether success is concealed:
public static int getMagicStats() { try { PackageManager pm = ApplicationEx.getInstance().getPackageManager(); ComponentName cn = new ComponentName(ApplicationEx.getInstance(), SplashActivity.class); int enableStatus = pm.getComponentEnabledSetting(cn); if (BuildConfig.DEBUG) { Log.d("magic_feature", "getMagicStats:" + (enableStatus == PackageManager.COMPONENT_ENABLED_STATE_ENABLED ? "enabled" : enableStatus == PackageManager.COMPONENT_ENABLED_STATE_DISABLED ? "disable" : ("unknown(" + enableStatus + ")"))); } return enableStatus; } catch (Exception e) { e.printStackTrace(); } return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT; }
All the preparatory work has been completed. This set of ideas that users can't uninstall is to create a shortcut like the system icon in the breath screen, hide the system icon, the shortcut Activity extends SplashActivity, users can only enter the product through the shortcut. For Xiaobai, the only way to uninstall the shortcut is to remove the option, uninstall should Procedures can only be processed in settings. It's enough to point out that the method of hiding icons will delay a few seconds. When hiding, if the current activity is our product, it will obviously see the interface zoomed back, so trigger it on the screen, and then hide these shortcomings.
Attached is the download address for Creating Shortcut Tool Class: https://download.csdn.net/download/ljyly525/10650634