即使在Android中破坏了活动,如何使按钮的SOS起作用



嗨,我的任务是制作按钮(即使活动被摧毁,也仍然可以看到和功能。我尝试使用服务并在创建活动时运行该服务。我动态创建按钮,然后将其放在窗口管理器的窗口中。为此写功能。代码如下:

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;
    }
}

您可以添加到params floatview(例如WindowManager(吗?您必须添加参数选项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); 

最新更新