I have a float icon that stays on top of all activities, but when the device get locked it disappear until the device get unlocked.
Another meaning, I want to display a view (FloatIcon) on lock screen using windowmanager from service.
This is my code so far.
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);
}
}
I could not get something useful when I google it.