系统本身自带有系统水印的源码,效果图和代码都列在下面,把if(safeMode)注释掉可以实现如下效果,可以参考着进行修改,有个问题就是这个自带的功能是不能同步切换语言,切换语言后需要重启手机才能生效
//frameworks/base/services/java/com/android/server/SystemServer.java
......
if (safeMode) {
mActivityManagerService.showSafeModeOverlay();
}
......
//frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
.......
public final void showSafeModeOverlay() {
View v = LayoutInflater.from(mContext).inflate(
com.android.internal.R.layout.safe_mode, null);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY;
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.BOTTOM | Gravity.START;
lp.format = v.getBackground().getOpacity();
lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
((WindowManager)mContext.getSystemService(
Context.WINDOW_SERVICE)).addView(v, lp);
}
......
实现水印同步切换语言最佳的解决方案如下
public final void showSafeModeOverlay() {
View v = LayoutInflater.from(mContext).inflate(
com.android.internal.R.layout.safe_mode, null);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY;
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.BOTTOM | Gravity.START;
lp.format = v.getBackground().getOpacity();
lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
((WindowManager)mContext.getSystemService(
Context.WINDOW_SERVICE)).addView(v, lp);
final Locale locale = mContext.getResources().getConfiguration().getLocales().get(0);
mContext.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(!mContext.getResources().getConfiguration().getLocales().get(0).equals(locale)) {
((WindowManager) mContext.getSystemService(
Context.WINDOW_SERVICE)).removeView(v);
mContext.unregisterReceiver(this);
showSafeModeOverlay();
}
}
},new IntentFilter(Intent.ACTION_LOCALE_CHANGED));
}