安卓:静态字符串得到最后呼出()方法 [英] Android : static String get Last Outgoing Call() method

查看:131
本文介绍了安卓:静态字符串得到最后呼出()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用静态字符串getLastOutgoingCall()方法,以拉最近呼出电话的时间,但我不知道怎么办!
我与Java编程(我通常程序C ++)

I'd like to use the static String getLastOutgoingCall() method in order to pull the duration of the last outgoing phone call but I don't know how ! I'm a beginner with java programming (I usually program in c++)

这是我发现使用古老的API和他们没有用我说的方法的教程。

The tutorials that I found use the ancient APIs and none of them use the method I'm talking about.

推荐答案

我希望我没有misinter preTED你的问题。如果是这样,请让我知道。

I hope I have not misinterpreted your question. If so, please let me know.

该方法字符串getLastOutgoingCall(上下文的背景下) android.provider.CallLog.Calls ,根据<一个href=\"http://developer.android.com/reference/android/provider/CallLog.Calls.html#getLastOutgoingCall(android.content.Context)\"相对=nofollow>文档,返回

最后一个电话号码拨打(拨出)或一个空字符串如果没有
  还存在

The last phone number dialed (outgoing) or an empty string if none exist yet.

因此​​,使用这种方法,你不能检索最近呼出通话时间。

So, you can't retrieve the last outgoing call duration using that method.

要获得最后拨出电话时长,您可以查询 CallLog.Calls.CONTENT_URI 来获取这些信息。

To get the last outgoing call duration, you can query the CallLog.Calls.CONTENT_URI to retrieve this info.

您可以使用的方法是这样的:

You can use a method like this:

public String getLastOutgoingCallDuration(final Context context) {
    String output = null;

    final Uri callog = CallLog.Calls.CONTENT_URI;
    Cursor cursor = null;

    try {
        // Query all the columns of the records that matches "type=2"
        // (outgoing) and orders the results by "date"
        cursor = context.getContentResolver().query(callog, null,
                CallLog.Calls.TYPE + "=" + CallLog.Calls.OUTGOING_TYPE,
                null, CallLog.Calls.DATE);
        final int durationCol = cursor
                .getColumnIndex(CallLog.Calls.DURATION);

        // Retrieve only the last record to get the last outgoing call
        if (cursor.moveToLast()) {
            // Retrieve only the duration column
            output = cursor.getString(durationCol);
        }
    } finally {
        // Close the resources
        if (cursor != null) {
            cursor.close();
        }
    }

    return output;
}

注意:要执行这个查询,你需要将下列权限添加到您的清单:

Note: To perform this query you will need to add the following permission to your manifest:

<uses-permission android:name="android.permission.READ_CALL_LOG" />


根据你自己的答案编辑:


Edit based on your own answer:

您需要调用 getLastOutgoingCallDuration()您的活动的的onCreate()方法:

You need to call the getLastOutgoingCallDuration() on the onCreate() method of your Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); // Here you need to set the name of your xml

    TextView displayDuration;
    displayDuration = (TextView)  findViewById(R.id.textView2);

    String duration = getLastOutgoingCallDuration(this);

    displayDuration.setText(output + "sec");
}

这篇关于安卓:静态字符串得到最后呼出()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆