如何在我的Android应用中显示应用覆盖屏幕?

时间:2019-07-15 08:44:18

标签: android screen overlay

嗨,我想在我的Android应用程序中显示一个应用程序覆盖屏幕。就像屏幕上显示停工消息。此屏幕应覆盖我的应用程序中的其他片段/活动。我怎么能做到这一点?

1 个答案:

答案 0 :(得分:0)

**1. Grant overlay permission of your appp that can show overlay layout.**

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)){
    private void systemOverlay(){
        //If the draw over permission is not available open the settings screen
        //to grant the permission.
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, CODE_DRAW_OVER_OTHER_APP_PERMISSION);
    }}


**2. Start Overlay Service**
    Intent intent = new Intent(this, NotificationHeadService.class);
    Bundle bundle = new Bundle();
    intent.putExtra("pNewsId", pNewsId);
    startService(intent);

**3. Layout**
remove.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/remove_relativelayout"
    android:background="@drawable/chat_background">

    <RelativeLayout
        android:layout_width="55dp"
        android:layout_height="55dp"
        android:layout_centerInParent="true"
        android:background="@drawable/chat_head_cross_bac">

        <ImageView
            android:layout_width="34dp"
            android:layout_height="34dp"
            android:id="@+id/remove_img"
            android:layout_centerInParent="true"
            android:src="@drawable/ic_cross_chat_head"
            />

    </RelativeLayout>




</RelativeLayout>

notificationhead.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="67dp"
    android:layout_height="67dp"
    android:id="@+id/chathead_relativelayout"
    android:background="@drawable/chat_corner"
    >

    <ImageView
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:id="@+id/chathead_img"
        android:adjustViewBounds="true"
        android:src="@drawable/ic_chat_head"
        android:visibility="visible"
        android:layout_centerInParent="true"
        android:padding="2dp"
        android:background="@drawable/chat_round_corner"
        />


</RelativeLayout>

3. Implementaion Like facebook messenger circle notification 

import java.util.Date;


public class NotificationHeadService extends Service {

    private WindowManager windowManager;
    private RelativeLayout chatheadView, removeView;
    private LinearLayout txtView, txt_linearlayout;
    private ImageView chatheadImg, removeImg;
    private int x_init_cord, y_init_cord, x_init_margin, y_init_margin;
    private Point szWindow = new Point();
    private boolean isLeft = true;
    private String sMsg = "";
    Bundle bundle = null;

    String pNewsId = "";
    String pushType = "";
    String pNewsImage = "";
    String pNewsPaperId = "";
    String custom_content="";

    int LAYOUT_FLAG;


    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
    private void handleStart(){
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
        }

        removeView = (RelativeLayout)inflater.inflate(R.layout.remove, null);

        final WindowManager.LayoutParams paramRemove = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                LAYOUT_FLAG,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                PixelFormat.TRANSLUCENT);
        paramRemove.gravity = Gravity.TOP | Gravity.LEFT;

        removeView.setVisibility(View.GONE);
        removeImg = (ImageView)removeView.findViewById(R.id.remove_img);
        windowManager.addView(removeView, paramRemove); //Ad Remove vIew


        chatheadView = (RelativeLayout) inflater.inflate(R.layout.notificationhead, null);
        chatheadImg = (ImageView)chatheadView.findViewById(R.id.chathead_img);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            windowManager.getDefaultDisplay().getSize(szWindow);
        } else {
            int w = windowManager.getDefaultDisplay().getWidth();
            int h = windowManager.getDefaultDisplay().getHeight();
            szWindow.set(w, h);
        }

        final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                LAYOUT_FLAG,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.TOP | Gravity.LEFT;


        params.x = 0;
        params.y = 150;
        windowManager.addView(chatheadView, params);
        chatheadView.setOnTouchListener(new View.OnTouchListener() {
            long time_start = 0, time_end = 0;
            boolean isLongclick = false, inBounded = false;
            int remove_img_width = 0, remove_img_height = 0;

            Handler handler_longClick = new Handler();
            Runnable runnable_longClick = new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    isLongclick = true;
                    removeView.setVisibility(View.VISIBLE);
                    chathead_longclick();
                }
            };

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) chatheadView.getLayoutParams();

                int x_cord = (int) event.getRawX();
                int y_cord = (int) event.getRawY();
                int x_cord_Destination, y_cord_Destination;

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        time_start = System.currentTimeMillis();
                        handler_longClick.postDelayed(runnable_longClick, 200);

                        remove_img_width = removeImg.getLayoutParams().width;
                        remove_img_height = removeImg.getLayoutParams().height;

                        x_init_cord = x_cord;
                        y_init_cord = y_cord;

                        x_init_margin = layoutParams.x;
                        y_init_margin = layoutParams.y;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int x_diff_move = x_cord - x_init_cord;
                        int y_diff_move = y_cord - y_init_cord;

                        x_cord_Destination = x_init_margin + x_diff_move;
                        y_cord_Destination = y_init_margin + y_diff_move;

                        if(isLongclick){
                            int x_bound_left = szWindow.x / 2 - (int)(remove_img_width * 1.5);
                            int x_bound_right = szWindow.x / 2 +  (int)(remove_img_width * 1.5);
                            int y_bound_top = szWindow.y - (int)(remove_img_height * 1.5);

                            if((x_cord >= x_bound_left && x_cord <= x_bound_right) && y_cord >= y_bound_top){
                                inBounded = true;

                                int x_cord_remove = (int) ((szWindow.x - (remove_img_height * 1.5)) / 2);
                                int y_cord_remove = (int) (szWindow.y - ((remove_img_width * 1.5) + getStatusBarHeight() ));

                                if(removeImg.getLayoutParams().height == remove_img_height){
                                    removeImg.getLayoutParams().height = (int) (remove_img_height * 1.5);
                                    removeImg.getLayoutParams().width = (int) (remove_img_width * 1.5);

                                    WindowManager.LayoutParams param_remove = (WindowManager.LayoutParams) removeView.getLayoutParams();
                                    param_remove.x = x_cord_remove;
                                    param_remove.y = y_cord_remove;

                                    windowManager.updateViewLayout(removeView, param_remove);
                                }

                                layoutParams.x = x_cord_remove + (Math.abs(removeView.getWidth() - chatheadView.getWidth())) / 2;
                                layoutParams.y = y_cord_remove + (Math.abs(removeView.getHeight() - chatheadView.getHeight())) / 2 ;

                                windowManager.updateViewLayout(chatheadView, layoutParams);
                                break;
                            }else{
                                inBounded = false;
                                removeImg.getLayoutParams().height = remove_img_height;
                                removeImg.getLayoutParams().width = remove_img_width;

                                WindowManager.LayoutParams param_remove = (WindowManager.LayoutParams) removeView.getLayoutParams();
                                int x_cord_remove = (szWindow.x - removeView.getWidth()) / 2;
                                int y_cord_remove = szWindow.y - (removeView.getHeight() + getStatusBarHeight() );

                                param_remove.x = x_cord_remove;
                                param_remove.y = y_cord_remove;

                                windowManager.updateViewLayout(removeView, param_remove);
                            }

                        }


                        layoutParams.x = x_cord_Destination;
                        layoutParams.y = y_cord_Destination;

                        windowManager.updateViewLayout(chatheadView, layoutParams);
                        break;
                    case MotionEvent.ACTION_UP:
                        isLongclick = false;
                        removeView.setVisibility(View.GONE);
                        removeImg.getLayoutParams().height = remove_img_height;
                        removeImg.getLayoutParams().width = remove_img_width;
                        handler_longClick.removeCallbacks(runnable_longClick);

                        if(inBounded){
                            stopService(new Intent(NotificationHeadService.this, NotificationHeadService.class));
                            inBounded = false;
                            break;
                        }


                        int x_diff = x_cord - x_init_cord;
                        int y_diff = y_cord - y_init_cord;

                        if(Math.abs(x_diff) < 5 && Math.abs(y_diff) < 5){
                            time_end = System.currentTimeMillis();
                            if((time_end - time_start) < 300){
                                windowManager.removeView(chatheadView);
                                chathead_click();
                            }
                        }
                        else {

                            y_cord_Destination = y_init_margin + y_diff;

                            int BarHeight = getStatusBarHeight();
                            if (y_cord_Destination < 0) {
                                y_cord_Destination = 0;
                            } else if (y_cord_Destination + (chatheadView.getHeight() + BarHeight) > szWindow.y) {
                                y_cord_Destination = szWindow.y - (chatheadView.getHeight() + BarHeight);
                            }
                            layoutParams.y = y_cord_Destination;
                        }

                        inBounded = false;
                        resetPosition(x_cord);

                        break;
                    default:
                        break;
                }
                return true;
            }
        });

    }


    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);

        if(windowManager == null)
            return;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            windowManager.getDefaultDisplay().getSize(szWindow);
        } else {
            int w = windowManager.getDefaultDisplay().getWidth();
            int h = windowManager.getDefaultDisplay().getHeight();
            szWindow.set(w, h);
        }

        WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) chatheadView.getLayoutParams();

        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            if(txtView != null){
                txtView.setVisibility(View.GONE);
            }

            if(layoutParams.y + (chatheadView.getHeight() + getStatusBarHeight()) > szWindow.y){
                layoutParams.y = szWindow.y- (chatheadView.getHeight() + getStatusBarHeight());
                windowManager.updateViewLayout(chatheadView, layoutParams);
            }

            if(layoutParams.x != 0 && layoutParams.x < szWindow.x){
                resetPosition(szWindow.x);
            }

        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            if(txtView != null){
                txtView.setVisibility(View.GONE);
            }

            if(layoutParams.x > szWindow.x){
                resetPosition(szWindow.x);
            }

        }

    }

    private void resetPosition(int x_cord_now) {

        if (x_cord_now <= szWindow.x / 2) {
            isLeft = true;
            moveToLeft(x_cord_now);

        } else {
            isLeft = false;
            moveToRight(x_cord_now);

        }

    }
    private void moveToLeft(final int x_cord_now){
        final int x = szWindow.x - x_cord_now;

        new CountDownTimer(500, 5) {
            WindowManager.LayoutParams mParams = (WindowManager.LayoutParams) chatheadView.getLayoutParams();
            public void onTick(long t) {
                if (chatheadView.getWindowToken() != null) {
                    long step = (500 - t) / 5;
                    mParams.x = 0 - (int) (double) bounceValue(step, x);
                    windowManager.updateViewLayout(chatheadView, mParams);
                }
            }
            public void onFinish() {
                if (chatheadView.getWindowToken() != null) {
                    mParams.x = 0;
                    windowManager.updateViewLayout(chatheadView, mParams);
                }
            }
        }.start();
    }
    private  void moveToRight(final int x_cord_now){
        new CountDownTimer(500, 5) {
            WindowManager.LayoutParams mParams = (WindowManager.LayoutParams) chatheadView.getLayoutParams();
            public void onTick(long t) {
                if (chatheadView.getWindowToken() != null) {
                    long step = (500 - t) / 5;
                    mParams.x = szWindow.x + (int) (double) bounceValue(step, x_cord_now) - chatheadView.getWidth();
                    windowManager.updateViewLayout(chatheadView, mParams);
                }
            }
            public void onFinish() {
                if (chatheadView.getWindowToken() != null) {
                    mParams.x = szWindow.x - chatheadView.getWidth();
                    windowManager.updateViewLayout(chatheadView, mParams);
                }
            }
        }.start();
    }

    private double bounceValue(long step, long scale){
        double value = scale * Math.exp(-0.055 * step) * Math.cos(0.08 * step);
        return value;
    }

    private int getStatusBarHeight() {
        int statusBarHeight = (int) Math.ceil(25 * getApplicationContext().getResources().getDisplayMetrics().density);
        return statusBarHeight;
    }

    private void chathead_click(){
        PendingIntent contentIntent=null;
        Date now = new Date();
        long uniqueId = now.getTime();

        Intent intent = new Intent(this, PushDetailsActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        contentIntent = PendingIntent.getActivity(this,
                        (int) uniqueId, intent.putExtra("pNewsId", pNewsId)
                                .putExtra("pNewsPaperId", pNewsPaperId),
                        PendingIntent.FLAG_UPDATE_CURRENT);

        try {
            contentIntent.send();
        } catch (PendingIntent.CanceledException e) {
            e.printStackTrace();
        }
    }

    private void chathead_longclick(){
        WindowManager.LayoutParams param_remove = (WindowManager.LayoutParams) removeView.getLayoutParams();
        int x_cord_remove = (szWindow.x - removeView.getWidth()) / 2;
        int y_cord_remove = szWindow.y - (removeView.getHeight() + getStatusBarHeight() );

        param_remove.x = x_cord_remove;
        param_remove.y = y_cord_remove;

        windowManager.updateViewLayout(removeView, param_remove);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        if(intent != null){
            final Bundle bundle = intent.getExtras();

            pNewsId = bundle.getString("pNewsId", "");
            pushType = bundle.getString("pushType", "");;
            pNewsImage = bundle.getString("pNewsImage", "");
            pNewsPaperId = bundle.getString("pNewsPaperId", "");
            custom_content="";

            Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            RingtoneManager.getRingtone(this, alarmSound).play();
        }

        if(startId == Service.START_STICKY) {
            handleStart();
            return super.onStartCommand(intent, flags, startId);
        }else{
            return  Service.START_NOT_STICKY;
        }

    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();

        if(chatheadView != null && chatheadView.getWindowToken() != null){
            windowManager.removeView(chatheadView);
        }

        if(txtView != null && chatheadView.getWindowToken() != null){
            windowManager.removeView(txtView);
        }

        if(removeView != null && chatheadView.getWindowToken() != null){
            windowManager.removeView(removeView);
        }

    }

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

}

链接:https://www.androidhive.info/2016/11/android-floating-widget-like-facebook-chat-head/