Android Service中弹出对话框

背景

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

解决方案:

1、添加权限:



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、对话框布局代码




    

        

        

        
    

5、对话框样式

  

完毕!!!

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

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

你可能感兴趣的:(移动端,Android,android)