我有一个浮动图标,它位于所有活动之上,但是当设备被锁定时,它会消失,直到设备被解锁。
另一个含义,我想使用服务中的窗口管理器在锁定屏幕上显示一个视图(FloatIcon)。
到目前为止,这是我的代码。
public class FloatIcon extends Service {
private WindowManager windowManager;
private ImageView floatIcon;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
floatIcon = new ImageView(this);
floatIcon.setImageResource(R.drawable.ic_launcher);
floatIcon.setClickable(true);
floatIcon.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getBaseContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(intent);
}
});
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED + WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
+ WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
windowManager.addView(floatIcon, params);
}
@Override
public void onDestroy() {
super.onDestroy();
if (floatIcon != null)
windowManager.removeView(floatIcon);
}
}
当我用谷歌搜索时,我找不到有用的东西。