Android彈出Window提示框(基於Android6.0)

在項目中經常會需要應用彈出一些自定義的窗口,這時候Android自帶的系統Dialog就無法滿足我們的需求了,爲了滿足項目需求,我們可以使用Window來滿足這一需求。

首先我們新建一個項目,來到MainActivity的佈局文件,在這裏新建一個按鈕用於彈出Window

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_window"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="open window"/>

</LinearLayout>

接着我們完成window的佈局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:paddingLeft="10dp"
        android:background="@android:color/darker_gray"
        android:orientation="vertical"
        android:layout_width="300dp"
        android:layout_height="200dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="發件人——13110203356"
            android:textSize="18sp"
            android:textColor="@color/colorPrimary"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_marginTop="5dp"
            android:text="這裏是內容"
            android:textSize="15sp"
            android:textColor="@color/colorAccent"/>

        <Button
            android:id="@+id/btn_send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="按鈕"
            android:layout_gravity="right"/>

    </LinearLayout>

</LinearLayout>

這個佈局實際上類似於短信提示框,效果如下
這裏寫圖片描述
之後在MainActivity中完成彈出Window的邏輯,首先我們要知道在Android6.0之前,使用Window只需要聲明權限<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>即可,但是從Android6.0開始,應用要想彈出一個懸浮在任意App上的Window的話需要用戶手動爲這個應用設置overlays權限,這個權限就連運行時權限也無法拿到,必須要用戶前往手機的權限界面爲應用設置該權限,因此在彈出window之前我們首先要進行一個邏輯判斷,判斷用戶是否已經獲取過overlays權限,如果獲取了權限就直接彈出window,若沒有獲取過overlays權限則會自動將界面跳轉到權限設置界面,讓用戶決定是否提供該權限,最後,代碼如下

public class MainActivity extends AppCompatActivity
{
    private Button btn_window;
    private Button btn_send;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_window= (Button) findViewById(R.id.btn_window);
        btn_window.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M)
                {
                    //版本大於6.0則需要判斷是否獲取了overlays權限
                    if (!Settings.canDrawOverlays(MainActivity.this))
                    {
                        startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                                Uri.parse("package:"+getPackageName())),1);
                    }
                    else
                    {
                        openWindow();
                    }
                }
            }
        });
    }

    private void openWindow()
    {
        //獲取WindowManager實例
        final WindowManager windowManager= (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        //獲取窗口布局參數實例
        WindowManager.LayoutParams params=new WindowManager.LayoutParams();
        //設置窗口布局參數屬性
        params.width= WindowManager.LayoutParams.MATCH_PARENT;
        params.height=WindowManager.LayoutParams.MATCH_PARENT;
        //設置window的顯示特性
        params.flags=WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
        //設置窗口半透明
        params.format= PixelFormat.TRANSLUCENT;
        //設置窗口類型
        params.type=WindowManager.LayoutParams.TYPE_PHONE;
        //獲取窗口布局實例
        final View windowView=View.inflate(this,R.layout.window,null);
        //獲取窗口布局中的按鈕實例
        btn_send= (Button) windowView.findViewById(R.id.btn_send);
        //設置點擊事件
        btn_send.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                windowManager.removeView(windowView);
            }
        });
        //顯示窗口
        windowManager.addView(windowView,params);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        switch (requestCode)
        {
            case 1:
            {
                if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M)
                {
                    if (Settings.canDrawOverlays(this))
                    {
                        //若用戶開啓了overlay權限,則打開window
                        openWindow();
                    }
                    else
                    {
                        Toast.makeText(this,"不開啓overlay權限",Toast.LENGTH_SHORT).show();
                    }
                }
                break;
            }
        }
    }
}

最後的效果如下,當然別忘了在Android6.0後也是需要在Manifest文件中聲明權限的

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章