简体   繁体   中英

use textView.setText() for linearlayout in PhoneStateListener

I have a class extends PhoneStateListener to listen to phone state and I want to display my custom dialog (design as a XML layout), it work properly but I have a problem when I'm trying to call textView.setText() in onCallStateChanged() method for a textView in my custom dialog but nothing happen. Can you show me how to do that? Many thanks!

Here's my onCallStateChanged method and getCallLog() method:

 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;
    }

Here's my custom layout (dialog): Custom layout

You have created new textview for txtDuration and txtCost. Try to add those textviews in your Linearlayout ly. Hope it will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM