3

当有人打电话给我时,我想显示一些信息。我有服务。此服务已注册广播接收器。此接收器侦听 android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED。当我的手机解锁时,我可以显示 toast,但是当我的手机锁定并且有人打电话给我时,没有显示 toast。当有人打电话给我时,显示一些信息的最佳方式是什么?

更新:我创建了在电话响铃时打开的浮动窗口。这工作得很好,但是当手机被锁定并且有人打电话给我时,窗口没有显示。当我接听电话时,浮动窗口就在那里。这里有什么方法可以在锁屏来电屏幕上显示这个浮动窗口吗?这是我打开浮动窗口的方式:

new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    startService(new Intent(context, FloatingWindow.class));
                }
            }, 2000);

这是我的浮动窗口

public class FloatingWindow extends Service{

private WindowManager wm;
private LinearLayout ll;
private Button btnStop;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();

    wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    ll = new LinearLayout(this);
    btnStop = new Button(this);

    ViewGroup.LayoutParams btnParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    btnStop.setText("Stop");
    btnStop.setLayoutParams(btnParameters);

    LinearLayout.LayoutParams llParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    ll.setBackgroundColor(Color.argb(66, 255, 0, 0));
    ll.setLayoutParams(llParameters);

    final WindowManager.LayoutParams parameters = new WindowManager.LayoutParams(400, 150, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
    parameters.x = 0;
    parameters.y = 0;
    parameters.gravity = Gravity.CENTER | Gravity.CENTER;

    ll.addView(btnStop);
    wm.addView(ll, parameters);

    ll.setOnTouchListener(new View.OnTouchListener() {

        private WindowManager.LayoutParams updatedParameters = parameters;
        int x, y;
        float touchedX, touchedY;
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    x = updatedParameters.x;
                    y = updatedParameters.y;

                    touchedX = event.getRawX();
                    touchedY = event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    updatedParameters.x = (int) (x + (event.getRawX() - touchedX));
                    updatedParameters.y = (int) (y + (event.getRawY() - touchedY));

                    wm.updateViewLayout(ll, updatedParameters);
                    break;
                default:
                    break;
            }
            return false;
        }
    });

    btnStop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            wm.removeView(ll);
            stopSelf();
        }
    });
}

}

4

1 回答 1

1

好的,所以我在一年前解决了它,但是还没有人写过这个问题,所以我将给出我的解决方案。

这是我打开浮动窗口的方法:

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        context.startService(new Intent(context, FloatingWindow.class));
    }
}, 1000);

在这里,来自我的 FloatingWindow 服务的 onCreate

@Override
public void onCreate() {
    super.onCreate();

    wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    ll = new LinearLayout(this);
    btnStop = new Button(this);

    gestureDetector = new GestureDetector(this, new GestureListener());

    ViewGroup.LayoutParams btnParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    btnStop.setText("X");
    btnStop.setLayoutParams(btnParameters);

    ViewGroup.LayoutParams tvParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT);

    LinearLayout.LayoutParams llParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    ll.setBackgroundColor(Color.argb(255, 255, 0, 0));
    ll.setLayoutParams(llParameters);

    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.CENTER;

    View popup_view = ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.popup_window, ll, false);
    tvInfo = (TextView) popup_view.findViewById(R.id.message);
    tvInfo.setText(this.getMessage());

    ll.addView(popup_view);
    wm.addView(ll, params);



    ll.setOnTouchListener(new View.OnTouchListener() {

        private WindowManager.LayoutParams updatedParameters = params;
        int x, y;
        float touchedX, touchedY;
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            gestureDetector.onTouchEvent(event);
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    x = updatedParameters.x;
                    y = updatedParameters.y;

                    touchedX = event.getRawX();
                    touchedY = event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    updatedParameters.x = (int) (x + (event.getRawX() - touchedX));
                    updatedParameters.y = (int) (y + (event.getRawY() - touchedY));

                    wm.updateViewLayout(ll, updatedParameters);
                    break;
                default:
                    break;
            }
            return false;
        }
    });

    gestureDetector.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            stopSelf();
            return false;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            return true;
        }
    });       
}

即使在锁定屏幕上也能完美运行。

于 2017-10-26T07:02:48.570 回答