我的代码在大多数设备上都能正常工作,但在三星银河A5中却无法正常工作
我正在尝试在本机拨号器上显示我的自定义覆盖对话框,以便通过单击它可以将用户重定向到我的应用程序。很好,不知道为什么它不能在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);
}
});
}
Xml for PopUp视图:-
<?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);
不要忘记在清单中添加此权限
<uses-permission android:name="android.permission.ACTION_MANAGE_OVERLAY_PERMISSION" />
关于android - 自定义对话框无法在三星Galaxy A5的来电屏幕上显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48399606/