嗨我有一个任务,即使活动被破坏,按钮(图像视图,按钮,文本等)仍然可见并且功能正常。我尝试使用服务并在创建活动时运行该服务。我动态创建了按钮并将其放在Window Manager的窗口中。为此写了函数。 代码如下:
public class FloatingBubbleService extends Service {
WindowManager windowManager;
ImageView floatingFaceBubble;
public void onCreate() {
Intent i = new Intent(FloatingBubbleService.this,
PreHomeActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
super.onCreate();
floatingFaceBubble = new ImageView(this);
//a face floating bubble as imageView
floatingFaceBubble.setImageResource(R.drawable.sos_icon);
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
//here is all the science of params
final WindowManager.LayoutParams myParams = new
WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
myParams.gravity = Gravity.TOP | Gravity.LEFT;
myParams.x = 0;
myParams.y = 100;
// add a floatingfacebubble icon in window
windowManager.addView(floatingFaceBubble, myParams);
try {
//for moving the picture on touch and slide
floatingFaceBubble.setOnTouchListener(new View.OnTouchListener() {
WindowManager.LayoutParams paramsT = myParams;
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
private long touchStartTime = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
//remove face bubble on long press
if (System.currentTimeMillis() - touchStartTime >
ViewConfiguration.getLongPressTimeout() && initialTouchX ==
event.getX()) {
windowManager.removeView(floatingFaceBubble);
stopSelf();
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchStartTime = System.currentTimeMillis();
initialX = myParams.x;
initialY = myParams.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
myParams.x = initialX + (int) (event.getRawX() -
initialTouchX);
myParams.y = initialY + (int) (event.getRawY() -
initialTouchY);
windowManager.updateViewLayout(v, myParams);
break;
}
return false;
}
});
} catch (Exception e) {
e.printStackTrace();
}
floatingFaceBubble.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent not = new Intent(FloatingBubbleService.this,
CreateNotificationActivity.class);
not.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(not);
}
});
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
public int onStartCommand(Intent intent, int flags, int startId) {
if(intent.hasExtra("isToHide") &&
intent.getBooleanExtra("isToHide",Boolean.FALSE)){
floatingFaceBubble.setVisibility(View.GONE);
}
if(intent.hasExtra("isToShow") &&
intent.getBooleanExtra("isToShow",Boolean.TRUE)){
if(floatingFaceBubble!=null)
floatingFaceBubble.setVisibility(View.VISIBLE);
}
return Service.START_STICKY;
}
}
答案 0 :(得分:0)
你可以像ParaManager一样添加Params你的floatview吗? 你必须添加params选项floatview。
params = new WindowManager.LayoutParams(
70,
70,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.TOP;
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(testView, params);
inflateView = View.inflate(this, R.layout.floatwindow, null);
floatWindowParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);