我有一个浮动图标,它位于所有活动的顶部,但当设备锁定时,它会消失,直到设备解锁。
另一个意思是,我想使用服务中的windowmanager在锁屏上显示一个视图(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);
}
}
我在谷歌上找不到有用的东西。
使用WindowManager.LayoutParams.TYPE_SYSTEM_ERROR而不是WindowManager.LLayoutParams.TYPE_PHONE可以解决此问题。这将使视图能够监听触摸事件,。但是,您也可以使用WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,但它不会监听触摸事件
尽管问题得到了回答,但有一些解释:-使用TYPE_PHONE窗口和FLAG_SHOW_WHEN_LOCKED只有在该窗口是全屏的情况下才会显示在锁定屏幕上-请访问android参考,而使用TYPE_SYSTEM_ERROR窗型没有全屏限制,从而解决了问题。
正如@milosmns所要求的,解决这个问题的方法可以是在你的代码中添加一个延迟,比如说100ms,在来电屏幕上显示一个窗口会很有魅力。
不要在OnCreate本身中添加视图。相反,制作一个广播接收器并将意图传递给它。只有当屏幕锁定时,它才会显示您的图标。当屏幕解锁时,它就会消失。以下是代码片段。不要忘记在清单中列出您的服务。
public class FloatingIconService extends Service {
private BroadcastReceiver mReceiver;
private boolean isShowing = false;
private WindowManager windowManager;
public ImageView icon;
private WindowManager.LayoutParams params;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("onStart", "FloatingIconService");
return super.onStartCommand(intent,flags,startId);
}
@Override
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
//add icon and its properties
icon = new ImageView(this);
icon.setImageResource(R.drawable.ic_action_name);
icon.setClickable(true);
icon.setPadding(10, 10, 10, 10);
icon.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
Log.i("onStart", "FloatingIconService when long press on icon");
((Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE)).vibrate(1000);
Intent i = new Intent(getApplicationContext(), Dashboard.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
return true;
}
});
//set parameters for the icon
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.BOTTOM;
params.x=0;
params.y=0;
//Register receiver for determining screen off and if user is present
mReceiver = new FloatingIconReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
registerReceiver(mReceiver, filter);
}
private class FloatingIconReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//if screen is turn off show the icon
if (!isShowing) {
windowManager.addView(icon, params);
isShowing = true;
}
} else if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
//Handle resuming events if user is present/screen is unlocked remove the icon immediately
if (isShowing) {
windowManager.removeViewImmediate(icon);
isShowing = false;
}
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (mReceiver != null) {
unregisterReceiver(mReceiver);
}
//remove view if it is showing and the service is destroy
if (isShowing) {
windowManager.removeViewImmediate(icon);
isShowing = false;
}
super.onDestroy();
}
}
只有有人能帮我把这个图标浮起来。就像我们可以在使用FAB的活动中做到这一点一样。