桌面快捷方式 + 桌面动态图标(展示未读信息条数) + 悬浮窗 + 沉浸式状态栏
桌面快捷方式:
private void addShortcutToDesktop() { Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); // 不允许重复创建 shortcut.putExtra("duplicate", false); // 设置快捷方式的名字 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,"萨瓦迪卡"); // 设置快捷方式的图标 shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getActivity(),R.drawable.banner_bg)); // 设置意图和快捷方式关联程序 shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,new Intent(getActivity(), getActivity().getClass()).setAction(Intent.ACTION_MAIN)); // 发送广播 getActivity().sendBroadcast(shortcut); }
<activity-alias>方式实现动态改变桌面图标:
application标签下加入下面代码:
<activity-alias android:name="com.example.administrator.studyworkdemo.newsLuncherActivity" android:enabled="false" android:icon="@mipmap/yifu" android:label="@string/app_name" android:targetActivity=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity-alias>
name参数为
com.example.administrator.studyworkdemo.newsLuncherActivity 新的activity类全类名:
private void changeLuncher(String name) { PackageManager pm = activity.getPackageManager(); pm.setComponentEnabledSetting(activity.getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); pm.setComponentEnabledSetting(new ComponentName(activity, name), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); //Intent 重启 Launcher 应用 Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.addCategory(Intent.CATEGORY_DEFAULT); List<ResolveInfo> resolves = pm.queryIntentActivities(intent, 0); for (ResolveInfo res : resolves) { if (res.activityInfo != null) { ActivityManager am = (ActivityManager) activity.getSystemService(Service.ACTIVITY_SERVICE); am.killBackgroundProcesses(res.activityInfo.packageName); } } }
添加悬浮窗:
void addWindow(){ WindowManager.LayoutParams params = new WindowManager.LayoutParams(); params.height = WindowManager.LayoutParams.WRAP_CONTENT; params.width = WindowManager.LayoutParams.WRAP_CONTENT; params.type = WindowManager.LayoutParams.TYPE_TOAST; // params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; // params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY; // params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR; // params.type = WindowManager.LayoutParams.TYPE_PRIORITY_PHONE; params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; WindowManager wm = activity.getWindowManager(); // Activity // wm = (WindowManager) getSystemService(WINDOW_SERVICE); // Service wm.addView(View.inflate(activity, R.layout.item, null), params); }
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
小米手机默认会关闭APP悬浮窗权限,需要用户手动设置开启APP权限!
沉浸式状态栏 透明:
/** * 通过设置全屏,设置状态栏透明 * * @param activity */ private void fullScreen(Activity activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色 Window window = activity.getWindow(); View decorView = window.getDecorView(); //两个 flag 要结合使用,表示让应用的主体内容占用系统状态栏的空间 int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE; decorView.setSystemUiVisibility(option); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.setStatusBarColor(Color.TRANSPARENT); //导航栏颜色也可以正常设置 // window.setNavigationBarColor(Color.TRANSPARENT); } else { Window window = activity.getWindow(); WindowManager.LayoutParams attributes = window.getAttributes(); int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION; attributes.flags |= flagTranslucentStatus; // attributes.flags |= flagTranslucentNavigation; window.setAttributes(attributes); } } }
各种demo都简单列举一种,还有很多种更优的解决方案。会继续跟帖。