如何修复屏幕上的弹出窗口

时间:2016-04-28 11:16:35

标签: java android android-studio popup

嗨,在我的应用程序中,我想改变设备调用者屏幕的外观,现在(我可能错了)唯一的方法是在默认的android调用者屏幕上调用弹出窗口,因为你无法改变它。

当用户点击拨号但我的弹出窗口出现,然后被默认的来电者屏幕取代,我的弹出窗口就会出现在默认来电者屏幕后面。

如何在屏幕上修复弹出窗口,使其不被任何其他屏幕替换。

我的DialerFragment.java:

package com.heroicjokester.android.haid;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.support.v4.content.ContextCompat;

/**
 * Created by Ripcord on 01-Apr-16.
 */
public class DialerFragment extends Fragment {
private EditText mPhoneField;
private Button mDialButton;
private LinearLayout mLinearLayout;
private LayoutInflater mLayoutInflater;
private PopupWindow mPopupWindow;

//Requesting Permissions on Runtime.
final private int REQUEST_CODE_ASK_PERMISSIONS=0;

private void InitiateCall(){
    int hasCallPermission = ContextCompat.checkSelfPermission(getActivity(),Manifest.permission.READ_PHONE_STATE);
    if (hasCallPermission != PackageManager.PERMISSION_GRANTED){
        requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE},
                REQUEST_CODE_ASK_PERMISSIONS);
        return;
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults ){
   switch (requestCode){
       case REQUEST_CODE_ASK_PERMISSIONS:
           if (grantResults[0]==PackageManager.PERMISSION_GRANTED){
               //YAY! PERMISSION GRANTED
               InitiateCall();
           }else{
               //GD! PERMISSION DENIED
               Toast.makeText(getActivity(), R.string.permission_denied, Toast.LENGTH_SHORT).show();
           }
            break;
       default:
           super.onRequestPermissionsResult(requestCode, permissions, grantResults);

   }
}



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

}

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
    View v=inflater.inflate(R.layout.fragment_dialer,container,false);

    mPhoneField=(EditText) v.findViewById(R.id.input_pno);
    mDialButton=(Button) v.findViewById(R.id.dial_button);
    mLinearLayout=(LinearLayout) v.findViewById(R.id.popup_linearlayout);

    mDialButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            try {
                if (mPhoneField != null && (mPhoneField.getText().length() == 10 || mPhoneField.getText().length() == 11)) {
                    startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mPhoneField.getText())));
                } else if (mPhoneField != null && mPhoneField.getText().length() == 0) {
                    Toast.makeText(getActivity(), R.string.no_number_toast, Toast.LENGTH_SHORT).show();
                } else if (mPhoneField != null && mPhoneField.getText().length() < 10) {
                    Toast.makeText(getActivity(), R.string.wrong_number_toast, Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                Log.e("DialerAppActivity", "error: " + e.getMessage(), e);//Runtime error will be logged
            }

            mLayoutInflater = (LayoutInflater) getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            ViewGroup container = (ViewGroup) mLayoutInflater.inflate(R.layout.dialer_popup, null);

            mPopupWindow = new PopupWindow(container, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, false);
            mPopupWindow.showAtLocation(mLinearLayout, Gravity.NO_GRAVITY, 0, 0);

            container.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    Toast.makeText(getActivity(), R.string.hit_it, Toast.LENGTH_LONG).show();
                    return true;

                }
            });

        }
    });

    InitiateCall();

    return v;


}


}

1 个答案:

答案 0 :(得分:0)

发现此代码,它使用广播接收器 我得到一个“无法解决变量arg0和arg1”错误任何想法如何删除它们?

// onReceive function of the Broadcast Receiver
    public void onReceive(Context arg0, Intent arg1) {
        String state = arg1.getStringExtra(TelephonyManager.EXTRA_STATE);

        // Adds a view on top of the dialer app when it launches.
        if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
            wm = (WindowManager) getContext().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,
                    PixelFormat.TRANSPARENT);

            params1.height = 75;
            params1.width = 512;
            params1.x = 265;
            params1.y = 400;
            params1.format = PixelFormat.TRANSLUCENT;

            mLinearLayout = new LinearLayout(getContext());
            mLinearLayout.setBackgroundColor(Color.BLACK);
            mLinearLayout.setOrientation(LinearLayout.VERTICAL);

            wm.addView(mLinearLayout, params1);
        }

        // To remove the view once the dialer app is closed.
        if(arg1.getAction().equals("android.intent.action.PHONE_STATE")){
            state = arg1.getStringExtra(TelephonyManager.EXTRA_STATE);
            if(state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
                WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
                if(mLinearLayout!=null)
                {
                    wm.removeView(mLinearLayout);
                    mLinearLayout = null;
                }
            }
        }
    }