سؤال

I have a method call in onReceive method.

public void onReceive(Context context, Intent intent) {
        SaveCallLog();
}
public boolean SaveCallLog() 
    {
        info = new TelephonyInfo();
        imei1 = info.getImeiSIM1();

        imei2 = "";
        if (info.isDualSIM()) {
            imei2 = info.getImeiSIM2();
        }

        Uri uri = Uri.parse("content://call_log/calls");
        ContentResolver contentResolver = mContext.getContentResolver();

        // here change where condition to get only those call logs those are not sent before
        Cursor cursor = contentResolver.query(uri, null, null, null, "date DESC");
        {
            if (cursor != null && cursor.getCount() > 0) 
            {
                while (cursor.moveToNext()) 
                {
                    phNumber = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
                    callType = cursor.getString(cursor.getColumnIndex(CallLog.Calls.TYPE));
                    callDate = cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE));
                    callDuration = cursor.getString(cursor.getColumnIndex(CallLog.Calls.DURATION));

                    dir = null;
                    int dircode = Integer.parseInt(callType);
                    switch (dircode)  
                    {
                        case CallLog.Calls.OUTGOING_TYPE:
                            dir = "OUTGOING";
                            break;
                        case CallLog.Calls.INCOMING_TYPE:
                            dir = "INCOMING";
                            break;
                        case CallLog.Calls.MISSED_TYPE:
                            dir = "MISSED";
                            break;
                        default:
                            dir = "REJECTED";
                    }
                    // here save the call log in your database to send them later
                    //String QS = "call&ImeiNo=" + "imei1" + "&ph=" + phNumber + "&duration=" + callDuration + "&date=" + callDate + "&cType=" + dir + "";
                 }



    cursor.close();
            Log.e("cursor", "here it is");

        }
        return true;    
    }

---->While Debugging as I reached to
ContentResolver contentResolver = mContext.getContentResolver(); It swaps me out from this page.

I have tried too much to resolve these issues But can't understand the problem of occurring these problems

هل كانت مفيدة؟

المحلول

mContext is not initialized..SO it giving you NullPointerException.

change your code like this..

public void onReceive(Context context, Intent intent) {
    mContext=context;
    SaveCallLog();
}

add this permission..in manifest

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

Based on OP requirement..

change your code like this and try..

// here change where condition to get only those call logs those are not
    // sent before
    Cursor cursor = contentResolver.query(uri, null, null, null,
            "date DESC");

    try {
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                do {
                    phNumber = cursor.getString(cursor
                            .getColumnIndex(CallLog.Calls.NUMBER));
                    callType = cursor.getString(cursor
                            .getColumnIndex(CallLog.Calls.TYPE));
                    callDate = cursor.getString(cursor
                            .getColumnIndex(CallLog.Calls.DATE));
                    callDuration = cursor.getString(cursor
                            .getColumnIndex(CallLog.Calls.DURATION));

                    dir = null;
                    int dircode = Integer.parseInt(callType);
                    switch (dircode) {
                    case CallLog.Calls.OUTGOING_TYPE:
                        dir = "OUTGOING";
                        break;
                    case CallLog.Calls.INCOMING_TYPE:
                        dir = "INCOMING";
                        break;
                    case CallLog.Calls.MISSED_TYPE:
                        dir = "MISSED";
                        break;
                    default:
                        dir = "REJECTED";
                    }
                } while (cursor.moveToNext());
            }
        }
    } catch (Exception e) {
        // TODO: handle exception
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top