如何在电话未接来电时显示举报状态?

时间:2011-08-05 13:09:47

标签: android

当我接到电话或未接来电时,我试图找出刚刚在手机上发生的CallLog和事件。

当我运行我的应用程序并且有未接来电时,我的应用程序是否可以显示吐司状态?

1 个答案:

答案 0 :(得分:1)

关于anddev的类似问题

http://www.anddev.org/other-coding-problems-f5/miss-call-alert-in-android-t3959.html

编辑:根据评论中的要求

import java.text.DateFormat;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.util.Log;

public class CallLogActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String[] strFields = {
                android.provider.CallLog.Calls.CACHED_NAME, android.provider.CallLog.Calls.NUMBER,
                android.provider.CallLog.Calls.DATE, android.provider.CallLog.Calls.TYPE
        };
        String strOrder = android.provider.CallLog.Calls.DATE + " DESC";

        Cursor mCallCursor = getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI,
                strFields, null, null, strOrder);

        if (mCallCursor.moveToFirst()) {

            do {

                boolean missed = mCallCursor.getInt(mCallCursor.getColumnIndex(CallLog.Calls.TYPE)) == CallLog.Calls.MISSED_TYPE;

                if (missed) {

                    String name = mCallCursor.getString(mCallCursor
                            .getColumnIndex(CallLog.Calls.CACHED_NAME));

                    String number = mCallCursor.getString(mCallCursor
                            .getColumnIndex(CallLog.Calls.NUMBER));

                    String time = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG)
                            .format(mCallCursor.getLong(mCallCursor
                                    .getColumnIndex(CallLog.Calls.DATE)));

                    Log.d("PhoneLog", "You have a missed call from " + name + " on " + number
                            + " at " + time);
                }

            } while (mCallCursor.moveToNext());

        }

    }
}

确保您拥有

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