如何从通话记录中拨打电话

时间:2018-08-13 13:48:32

标签: android call phone-call calllog

我需要能够从通话记录中拨打号码的帮助。我需要使用户能够单击其中一个号码并拨打该号码。我想我需要建立一个数组,但我真的不知道,或者数字应该成为链接?。

public class MainActivity extends AppCompatActivity {

    TextView call;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        call = (TextView)findViewById(R.id.call); //finds textview
        getCallDetails();
        final TextView dialers=(TextView)this.findViewById(R.id.call);
        dialers.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Intent.ACTION_DIAL,
                        Uri.parse("tel://"+ dialers.getText().toString().trim())));
            }
        });

    }
    private void getCallDetails() {

        StringBuffer sb = new StringBuffer();
        Cursor managedCursor = managedQuery( CallLog.Calls.CONTENT_URI,null, null,null, null);
        int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER );
        int type = managedCursor.getColumnIndex( CallLog.Calls.TYPE );
        int date = managedCursor.getColumnIndex( CallLog.Calls.DATE);
        int duration = managedCursor.getColumnIndex( CallLog.Calls.DURATION);
        sb.append( "Call Details :");
        while ( managedCursor.moveToNext() ) {
            String phNumber = managedCursor.getString( number );
            String callType = managedCursor.getString( type );
            String callDate = managedCursor.getString( date );
            Date callDayTime = new Date(Long.valueOf(callDate));
            String callDuration = managedCursor.getString( duration );
            String dir = null;


            int dircode = Integer.parseInt( callType );
            switch( dircode ) {
                case CallLog.Calls.OUTGOING_TYPE:
                    dir = "OUTGOING";
                    break;
                    //gets outcoming calls

                case CallLog.Calls.INCOMING_TYPE:
                    dir = "INCOMING";
                    break;
                    //gets incoming calls

                case CallLog.Calls.MISSED_TYPE:
                    dir = "MISSED";
                    break;

            }
            sb.append( "\nPhone Number:--- "+phNumber +" \nCall Type:--- "+dir+" \nCall Date:--- "+callDayTime+" \nCall duration in sec :--- "+callDuration );
            sb.append("\n----------------------------------");


        }
        managedCursor.close();
        call.setText(sb);
    }
}

1 个答案:

答案 0 :(得分:0)

将这些属性添加到call TextView中,在其中显示电话号码:

            android:autoLink="phone"
            android:linksClickable="true"

,数字将带有链接颜色,当您单击数字时,将启动拨号应用程序。


call.setText(sb);
call.setLinkTextColor(Color.RED);
Linkify.addLinks(call, Linkify.PHONE_NUMBERS);