如何在服务中创建另一个之前解除WindowManager PopUp(Dialog)?

时间:2018-04-27 05:12:43

标签: android service broadcastreceiver android-windowmanager

每当我错过电话时,我都会显示WindowManager PopUp(Dialog)。当发生超过1次未接来电时,则关闭按钮无法解除WindowManager PopUp(对话框)。为什么它不工作以及如何在创建新的WindowManager PopUp(Dialog)之前关闭它。下面是我创建WindowManager PopUp(Dialog)的代码。我在我的Services类中创建它。当我错过了电话时,我就开始服务了。

////// show dialog...
public void dialogIncomingRingScreen(String namee){


    windowManager2 = (WindowManager)getSystemService(WINDOW_SERVICE);

    LayoutInflater layoutInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view=layoutInflater.inflate(R.layout.pup_up_incoming_miss_end_call, null);
    params=new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED + WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                    + WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

    params.gravity= Gravity.CENTER|Gravity.CENTER;
    params.x=0;
    params.y=0;
    windowManager2.addView(view, params);

    view.setOnTouchListener(new View.OnTouchListener() {
        private int initialX;
        private int initialY;
        private float initialTouchX;
        private float initialTouchY;

        @Override public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    initialX = params.x;
                    initialY = params.y;
                    initialTouchX = event.getRawX();
                    initialTouchY = event.getRawY();
                    return true;
                case MotionEvent.ACTION_UP:
                    return true;
                case MotionEvent.ACTION_MOVE:
                    if(view !=null) {
                        params.x = initialX + (int) (event.getRawX() - initialTouchX);
                        params.y = initialY + (int) (event.getRawY() - initialTouchY);
                        windowManager2.updateViewLayout(view, params);
                    }
                    return true;
            }
            return false;
        }
    });


    callBtn = (Button) view.findViewById(R.id.buttonCall);
    callBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:"+number));
            startActivity(callIntent);
            clearView(v.getContext());
            stopSelf();
        }
    });
    messageBtn  = (Button)view.findViewById(R.id.buttonMessage);
    messageBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            Intent intent = new Intent(Intent.ACTION_SENDTO);
            intent.setData(Uri.parse("smsto:" + Uri.encode(number)));
            startActivity(intent);
            clearView(v.getContext());
            stopSelf();
        }
    });
    viewContactBtn = (Button)view.findViewById(R.id.buttonView);
    viewContactBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Context context = InComingCallEndServices.this;
            ContentResolver contentResolver = context.getContentResolver();

            Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

            String[] projection = new String[] {ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID};

            Cursor cursor =
                    contentResolver.query(
                            uri,
                            projection,
                            null,
                            null,
                            null);

            if(cursor!=null) {
                while(cursor.moveToNext()){
                    String contactName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
                    String contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    Uri urir = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactId));
                    intent.setData(urir);
                    context.startActivity(intent);
                }
                cursor.close();
            }



            clearView(v.getContext());
            stopSelf();

        }
    });
    txtNameIncoming = (TextView)view.findViewById(R.id.txtIncomingName);
    txtNameIncoming.setText(namee);

    txtIncomingnumber = (TextView)view.findViewById(R.id.textViewNumber);
    txtIncomingnumber.setText(number);
    btnClose = (Button) view.findViewById(R.id.buttonClose);

    btnClose.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            clearView(v.getContext());
      stopSelf();
        }
    });
    TextView logtxt = view.findViewById(R.id.textViewLastCall);
    txtAddress = view.findViewById(R.id.txtAddress);
    txtAddress.setText(countryCode(number));
    String num = getCallDetails();
    if(num!=null){
        logtxt.setText(num);
    }else {
        logtxt.setText("sss");
    }
}

下面的代码用于解除WindowManager PopUp(对话框)。

public static void clearView(Context context) {
    windowManager2 = (WindowManager) context.getSystemService(WINDOW_SERVICE);
    if(view != null) {
        if(view.isEnabled()){
            windowManager2.removeView(view);

            view = null;

        }
    }
}

0 个答案:

没有答案