android - 三星 Galaxy A5 中的自定义对话框未显示在来电屏幕上

标签 android service dialog flags android-windowmanager

我的代码在大多数设备上运行良好,但在 samsung galaxy A5 上运行不佳 我正在尝试在 native 拨号器上显示我的自定义覆盖对话框,以便通过单击它我可以将用户重定向到我的应用程序。我已经在 Smasung J7 prime、Samsung onNXT、Moto G4 plus 等中测试了我的代码 在每台设备上 都工作正常不知道为什么它不能在 Samsung Galaxy A5 上工作

这里有人面对同样的问题吗?

这是我的代码:-

private static WindowManager wm;
public static View popupView;
private static WindowManager.LayoutParams params1;

private static void showPopup(Context applicationContext) {
    wm = (WindowManager) applicationContext.getSystemService(Context.WINDOW_SERVICE);
    params1 = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT |
                    WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                    | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
            PixelFormat.TRANSPARENT);
    params1.height = WindowManager.LayoutParams.WRAP_CONTENT;
    params1.width = WindowManager.LayoutParams.MATCH_PARENT;
    params1.x = 0;
    params1.y = 0;
    params1.gravity = Gravity.TOP;
    params1.format = PixelFormat.TRANSLUCENT;
    LayoutInflater layoutInflater =
            (LayoutInflater) applicationContext
                    .getSystemService(applicationContext.LAYOUT_INFLATER_SERVICE);
    popupView = layoutInflater.inflate(R.layout.activity_dialog, null);
    int[] attrs = new int[]{R.attr.selectableItemBackground};
    TypedArray typedArray = applicationContext.obtainStyledAttributes(attrs);
    int backgroundResource = typedArray.getResourceId(0, 0);
    popupView.setBackgroundResource(backgroundResource);
    wm.addView(popupView, params1);
    popupView.setVisibility(View.GONE);
    popupView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popupView.setVisibility(View.GONE);
            startActivity(applicationContext);
        }
    });
}

弹出 View 的 Xml :-

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/rv_parentview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="2sp"
        android:background="@drawable/rounded_corner_bg">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal"
            android:padding="@dimen/text_size_very_small">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="@dimen/text_size_very_small"
                android:text="Add Call"
                style="@style/FontHeavyBold"
                android:textColor="@android:color/black"
                android:textSize="@dimen/text_size_large"/>

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/text_size_very_small"
                android:layout_marginRight="@dimen/text_size_very_small">

                <ImageView
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:layout_centerVertical="true"
                    android:background="@drawable/fill_circle_bg" />

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:src="@drawable/white_add_call" />

            </RelativeLayout>

            <!-- <TextView
                 style="@style/FontHeavyBold"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:paddingLeft="90dp"
                 android:paddingRight="90dp"
                 android:text="Add Call"
                 android:textAppearance="@style/FontLight"
                 android:textColor="@color/app_color"
                 android:textSize="@dimen/text_size_medium" />

             <ImageView
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_marginTop="@dimen/text_size_very_small"
                 android:src="@mipmap/ico_return" />-->

        </LinearLayout>
    </RelativeLayout>
</RelativeLayout>

最佳答案

将此权限检查添加到您的 onCreate()

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

    //Check if the application has draw over other apps permission or not?
    //This permission is by default available for API<23. But for API > 23
    //you have to ask for the permission in runtime.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&   !Settings.canDrawOverlays(getActivity())) {
        //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:" + getActivity().getPackageName()));
        startActivityForResult(intent, CODE_DRAW_OVER_OTHER_APP_PERMISSION);
    }

}

并添加onActivityResult()

private static final int CODE_DRAW_OVER_OTHER_APP_PERMISSION = 2084;

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CODE_DRAW_OVER_OTHER_APP_PERMISSION) {

        //Check if the permission is granted or not.
        if (resultCode == RESULT_OK) {

        }
        else
            {
                // Permission is not available
            }

    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

使用 AlertDialog 并将窗口类型更改为 TYPE_PHONE

 AlertDialog.Builder builder = new AlertDialog.Builder(context.getApplicationContext());
            LayoutInflater inflater = LayoutInflater.from(context);
            View dialogView = inflater.inflate(R.layout.caller_dialog, null);
            Window window = alert.getWindow();
            builder.setView(dialogView);
            final AlertDialog alert = builder.create();
            window.requestFeature(Window.FEATURE_NO_TITLE);
            window.setType(WindowManager.LayoutParams.TYPE_PHONE);
            alert.setCanceledOnTouchOutside(true);
            alert.show();
            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

            window.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
            window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            window.setGravity(Gravity.TOP);
            lp.copyFrom(window.getAttributes());
            //This makes the dialog take up the full width
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(lp);

不要忘记在 list 中添加此权限

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

关于android - 三星 Galaxy A5 中的自定义对话框未显示在来电屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48399606/

相关文章:

android - 为什么我的 Android SQLite 数据库有时无法加载数据库中的所有对象?

java - 将 JSON 发送到 Firebase 云消息传递

Android 设备监视器未打开 - 日志文件错误

android - 应用速度集成-Google Analytics(分析)

service - 查看服务是否安装

windows - 使用 powershell 写入 excel 时防止覆盖弹出窗口

Android 服务和 SQLite 访问数据完整性

ssl - CAS 服务票据验证失败

Android:是否存在即使将 "android:configChanges="设置为所有内容也重新创建 Activity 的情况?

android - 无法在未调用 Looper.prepare 的线程内创建处理程序