在PhoneStateListener中使用textView.setText()进行linearlayout

时间:2015-12-17 12:03:34

标签: java android android-layout android-view

我有一个类扩展PhoneStateListener来监听手机状态,我想显示我的自定义对话框(设计为XML布局),它工作正常,但是当我尝试调用textView.setText时遇到问题()在我的自定义对话框中的textView的onCallStateChanged()方法中,但没有任何反应。你能告诉我怎么做吗?非常感谢!

这是我的onCallStateChanged方法和getCallLog()方法:

 public void onCallStateChanged(int state, String incomingNumber) {
        if (incomingNumber != null && incomingNumber.length() > 0) {
            _incomingNumber = incomingNumber;
        }
       final WindowManager wm = (WindowManager) _context.getSystemService(Context.WINDOW_SERVICE);
        final LinearLayout ly;

        switch (state){
            case TelephonyManager.CALL_STATE_IDLE: {
                CallLog lastCall = getNewCallLog();
                if(lastCall.get_callDuration() >0) {
                    //I check my value here and it work properly
                    Toast.makeText(_context.getApplicationContext(),Integer.toString(lastCall.get_callDuration()) + "-" +Integer.toString(lastCall.get_callFee()),Toast.LENGTH_LONG).show();
                    WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
                            WindowManager.LayoutParams.WRAP_CONTENT,
                            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                            PixelFormat.TRANSPARENT);

                    params.height = WindowManager.LayoutParams.WRAP_CONTENT;
                    params.width = WindowManager.LayoutParams.WRAP_CONTENT;
                    params.format = PixelFormat.TRANSPARENT;
                    params.gravity = Gravity.TOP;


                    final LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    ly = (LinearLayout) inflater.inflate(R.layout.custom_dialog, null);
                    TextView txtDuration = (TextView)ly.findViewById(R.id.txt_duration);
                    TextView txtCost = (TextView)ly.findViewById(R.id.txt_cost);
                    Button yesBtn = (Button) ly.findViewById(R.id.btn_yes);
                    //Set value for textView here
                    txtDuration.setText(Integer.toString(lastCall.get_callDuration()));
                    txtDuration.setText(Integer.toString(lastCall.get_callDuration()));

                    txtCost.setText(Integer.toString(lastCall.get_callFee()));
                    yesBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            wm.removeView(ly);
                        }
                    });
                    wm.addView(ly, params);
                }
                break;
            }
            case TelephonyManager.CALL_STATE_OFFHOOK:
                break;
            case TelephonyManager.CALL_STATE_RINGING:

                break;
        }

    }
    public CallLog getNewCallLog()
    {
        CallLog _newCall = new CallLog();
        Cursor cursor = this._context.getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI, null, null,
                null, android.provider.CallLog.Calls.DATE + " DESC");
        int number = cursor.getColumnIndex(android.provider.CallLog.Calls.NUMBER);
        int callType = cursor.getColumnIndex(android.provider.CallLog.Calls.TYPE);
        int date = cursor.getColumnIndex(android.provider.CallLog.Calls.DATE);
        int duration = cursor.getColumnIndex(android.provider.CallLog.Calls.DURATION);

        if(cursor.moveToFirst() )
        {
            String _callType = cursor.getString(callType);
            int dircode = Integer.parseInt(_callType);
            if(dircode == android.provider.CallLog.Calls.OUTGOING_TYPE) {
                String _number = cursor.getString(number);
                String _callDate = cursor.getString(date);
                Date _callDayTime = new Date(Long.valueOf(_callDate));
                int _callDuration = cursor.getInt(duration);
                _package.set_callDuration(_callDuration);
                _package.set_callTime(DateTimeManager.get_instance().convertToHm(_callDayTime.toString()));
                _package.set_outGoingPhoneNumber(_number);
                int fee = _package.CalculateCallFee();
                _newCall.set_callDuration(_callDuration);
                _newCall.set_callFee(fee);
                return _newCall;
            }
        }
        return null;
    }

这是我的自定义布局(对话框): Custom layout

1 个答案:

答案 0 :(得分:0)

您已为txtDuration和txtCost创建了新的textview。尝试在Linearlayout中添加这些文本视图。希望它会对你有所帮助。