Android Service中弹出对话框

目录

背景

解决方案:

1、添加权限:

2、添加getApplicationContext()

3、对话框代码

4、对话框布局代码

5、对话框样式


背景

     dialog 对话框只提供Activity上下文显示环境,但是很多时候需要在后台服务中显示对话框的场景,例如后台收到哪个反馈时,弹出对应的对话框提示用户。

解决方案:

1、添加权限:

<!--services权限-->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />

2、添加getApplicationContext()

记得做判断8.0以上系统
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    Objects.requireNonNull(window).setType((WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY));
} else {
    Objects.requireNonNull(window).setType((WindowManager.LayoutParams.TYPE_SYSTEM_ALERT));
}
    private VoiceDialog boxDialog;

    private void showDialog() {

        View inflate = LayoutInflater.from(getApplicationContext()).inflate(R.layout.donghua_layout, null, false);
        boxDialog = new VoiceDialog(getApplicationContext(), inflate, VoiceDialog.LocationView.BOTTOM);
//        boxDialog.setCancelable(false);//是否可以点击DialogView外关闭Dialog
//        boxDialog.setCanceledOnTouchOutside(false);//是否可以按返回按钮关闭Dialog
        boxDialog.show();
        boxDialog.showVoiceLum();
    }

3、对话框代码


/**
 * 语音
 * 对话框
 */

public class VoiceDialog extends Dialog {

    //Dialog View
    private View view;
    //Dialog弹出位置
    private LocationView locationView = LocationView.BOTTOM;

    /**
     * @param context 上下文
     * @param view    Dialog View
     */
    public VoiceDialog(Context context, View view) {
        super(context, R.style.BoxDialog);
        this.view = view;
    }

    /**
     * @param context      上下文
     * @param view         Dialog View
     * @param locationView Dialog
     *                     弹出位置
     */
    public VoiceDialog(Context context, View view, LocationView locationView) {
        super(context, R.style.BoxDialog);
        this.view = view;
        this.locationView = locationView;
        waveLineView = view.findViewById(R.id.waveLineView);
        tvContView = view.findViewById(R.id.tv_content);
        closeDialog = view.findViewById(R.id.close_dialog);
        showTxtContent("请说话");
        clickListener();
    }

    @SuppressLint("RtlHardcoded")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (null != view) {
            setContentView(view);
            setCancelable(true);//点击外部是否可以关闭Dialog
            setCanceledOnTouchOutside(true);//返回键是否可以关闭Dialog

            Window window = this.getWindow();
            assert window != null;
            switch (locationView) {
                case TOP:
                    window.setGravity(Gravity.TOP);
                    break;
                case BOTTOM:
                    window.setGravity(Gravity.BOTTOM);
                    break;
                case CENTER:
                    window.setGravity(Gravity.CENTER);
                    break;
            }

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                Objects.requireNonNull(window).setType((WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY));
            } else {
                Objects.requireNonNull(window).setType((WindowManager.LayoutParams.TYPE_SYSTEM_ALERT));
            }

            window.setBackgroundDrawableResource(android.R.color.transparent);

            WindowManager.LayoutParams params = window.getAttributes();
            params.width = WindowManager.LayoutParams.WRAP_CONTENT;
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            params.y = 10;//设置与底部距离
            window.setAttributes(params);
        }
    }

    /**
     * 语音
     * 播放动画
     */
    private WaveLineView waveLineView;
    private TextView tvContView;
    private ImageView closeDialog;

    public void showVoiceLum() {
        if (waveLineView != null) {
            waveLineView.startAnim();
            waveLineView.onResume();
        }
    }

    /**
     * 语音
     * 停止动画
     */
    public void stopAminVoice() {
        if (waveLineView != null) {
            waveLineView.onPause();
            waveLineView.clearDraw();
        }
    }

    /**
     * 设置
     * 内容
     */
    public void showTxtContent(String ct) {
        if (tvContView != null) {
            tvContView.setText(ct);
        }

    }

    /**
     * 显示
     * 位置
     */
    public enum LocationView {
        CENTER, TOP, BOTTOM
    }

    /**
     * 事件
     * 监听
     */
    private void clickListener() {
        if (closeDialog != null) {
            closeDialog.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dismiss();
                }
            });
        }

    }

}

4、对话框布局代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/parent_layout"
        android:layout_width="@dimen/dp_160"
        android:layout_height="@dimen/dp_38"
        android:background="@drawable/kdvoice_shape">

        <ImageView
            android:id="@+id/close_dialog"
            android:layout_width="@dimen/dp_10"
            android:layout_height="@dimen/dp_10"
            android:layout_alignRight="@id/tv_content"
            android:layout_alignParentRight="true"
            android:layout_marginTop="@dimen/dp_4"
            android:layout_marginRight="@dimen/dp_8"
            android:src="@drawable/closed_icon" />

        <TextView
            android:id="@+id/tv_content"
            android:layout_width="@dimen/dp_140"
            android:layout_height="@dimen/dp_20"
           android:layout_marginLeft="@dimen/dp_5"
            android:ellipsize="end"
            android:gravity="center_horizontal"
            android:maxEms="26"
            android:paddingTop="@dimen/dp_3"
            android:singleLine="true"
            android:text="请说话"
            android:textColor="@color/color666"
            android:textSize="@dimen/sp_10" />

        <jaygoo.widget.wlv.WaveLineView
            android:id="@+id/waveLineView"
            android:layout_width="@dimen/dp_160"
            android:layout_height="@dimen/dp_18"
            android:layout_below="@id/tv_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="@dimen/dp_4"
            app:wlvBackgroundColor="@color/white"
            app:wlvFineLineWidth="@dimen/dp_0.2"
            app:wlvLineColor="@color/app_color_theme_5"
            app:wlvMoveSpeed="290"
            app:wlvThickLineWidth="@dimen/dp_0.5" />
    </RelativeLayout>

</RelativeLayout>

5、对话框样式

  <style name="BoxDialog" parent="@android:style/Theme.Holo.Dialog"> <!-- 是否有边框 -->
        <item name="android:windowFrame">@null</item> <!--是否在悬浮Activity之上 -->
        <item name="android:windowIsFloating">true</item> <!-- 标题 -->
        <item name="android:windowNoTitle">true</item> <!--阴影 -->
        <item name="android:windowIsTranslucent">true</item> <!--背景透明-->
        <item name="android:windowBackground">@android:color/transparent</item> <!--可加入动画-->
    </style>

完毕!!!

注意由于业务场景需要用到的语音播放效果库WaveLineView

 implementation 'com.github.Jay-Goo:WaveLineView:v1.0.4'

猜你喜欢

转载自blog.csdn.net/shi450561200/article/details/134728151