首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >来电屏幕上的弹出窗口,如truecaller

来电屏幕上的弹出窗口,如truecaller
EN

Stack Overflow用户
提问于 2014-05-17 02:04:47
回答 1查看 10.2K关注 0票数 3

我试图添加弹出式窗口的来电屏幕作为真正的呼叫者,但未能实现.let我知道这是什么逻辑背后,我可以如何实现这一点

代码语言:javascript
运行
复制
  public void onReceive(Context context, Intent intent) {

    try {
        // TELEPHONY MANAGER class object to register one listner
        TelephonyManager tmgr = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);


            }
    } catch (Exception e) {
        Log.e("Phone Receive Error", " " + e);
    }

}
EN

回答 1

Stack Overflow用户

发布于 2018-01-24 17:14:11

尝尝这个

代码语言:javascript
运行
复制
AlertDialog.Builder builder = new AlertDialog.Builder(context.getApplicationContext());
            LayoutInflater inflater = LayoutInflater.from(context);
            View dialogView = inflater.inflate(R.layout.caller_dialog, null);

            ImageView button = dialogView.findViewById(R.id.close_btn);

            builder.setView(dialogView);
            final AlertDialog alert = builder.create();
            alert.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            alert.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);
            alert.setCanceledOnTouchOutside(true);
            alert.show();
            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            Window window = alert.getWindow();
            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);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //close the service and remove the from from the window
                    alert.dismiss();
                }
            });

并在您的onCreate()中添加权限检查

代码语言:javascript
运行
复制
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()

代码语言:javascript
运行
复制
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);
    }
}

并且不要忘记在清单中添加权限

代码语言:javascript
运行
复制
<uses-permission android:name="android.permission.ACTION_MANAGE_OVERLAY_PERMISSION" />
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23701879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档