4

私は電話番号を取得する必要があるアプリを作成していますが、それを取得することはできますが、私の問題は、現在の通話ではなく最後の通話の電話番号を取得していることです。私のコードは次のとおりです。

projection = new String[]{Calls.NUMBER};
cur = context.getContentResolver().query(Calls.CONTENT_URI, projection, null, null, null);
cur.requery();
numberColumn = cur.getColumnIndex(Calls.NUMBER);
cur.requery();
cur.requery();
cur.requery();
cur.requery();
cur.moveToLast();
s = cur.getString(numberColumn);
String pathname = "/sdcard/" + s + ".amr";
4

2 に答える 2

15

Krutix が示唆するように、通話 LOG は、通話が終了した後にダイヤラ アプリによって書き込まれる、終了した通話の LOG です。そのため、コンテンツ プロバイダーで現在ダイヤルしている番号がわかりません。

これは、incomingNumber として電話がかかってきた場合、および電話が終了した場合にも電話番号を取得できるようにする実装です - Handler() コードに注意してください。

private class PhoneCallListener extends PhoneStateListener {

    private boolean isPhoneCalling = false;

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {

        if (TelephonyManager.CALL_STATE_RINGING == state) {
            // phone ringing
            Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
        }

        if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
            // active
            Log.i(LOG_TAG, "OFFHOOK");

            isPhoneCalling = true;
        }

        if (TelephonyManager.CALL_STATE_IDLE == state) {
            // run when class initial and phone call ended, need detect flag
            // from CALL_STATE_OFFHOOK
            Log.i(LOG_TAG, "IDLE number");

            if (isPhoneCalling) {

                Handler handler = new Handler();

                //Put in delay because call log is not updated immediately when state changed
                // The dialler takes a little bit of time to write to it 500ms seems to be enough
                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // get start of cursor
                          Log.i("CallLogDetailsActivity", "Getting Log activity...");
                            String[] projection = new String[]{Calls.NUMBER};
                            Cursor cur = getContentResolver().query(Calls.CONTENT_URI, projection, null, null, Calls.DATE +" desc");
                            cur.moveToFirst();
                            String lastCallnumber = cur.getString(0);
                    }
                },500);

                isPhoneCalling = false;
            }

        }
    }
}

次に、onCreate または onStartCommand コードにリスナーを追加して初期化します。

    PhoneCallListener phoneListener = new PhoneCallListener();
    TelephonyManager telephonyManager = (TelephonyManager) this
            .getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(phoneListener,
            PhoneStateListener.LISTEN_CALL_STATE);
于 2012-06-25T02:02:15.987 に答える
1

クエリを変更してください

 cur = context.getContentResolver().query(Calls.CONTENT_URI,
                        projection, null, null, Calls.DATE +" desc");
  cur.moveToFirst();
  s = cur.getString(numberColumn);

それは最後の呼び出しの電話番号を与えるでしょう、そしてそれは何度も再クエリする必要はありません

于 2012-04-13T07:16:19.410 に答える