0

我正在尝试跟踪电话状态并记录。我需要电话号码、姓名(如果是已保存的联系人)以及通话时间和持续时间。问题是getContentResolver()无法调用该方法,它在代码中进行了注释。

public class PhoneStateBroadcastReciever extends BroadcastReceiver {
Context m_context;

String m_number = null;
String m_startTime = null;
String m_endTime = null;

SharedPreferences m_sharedPrefs;
Editor editor;

static String PREFS_NUMBER;
static String PREFS_START_TIME;
static String PREFS_END_TIME;



 @Override
    public void onReceive(Context context, Intent intent) {
    m_sharedPrefs = m_context.getSharedPreferences("MyPrefs", 0);
    editor = m_sharedPrefs.edit();

    Bundle bundle = intent.getExtras();
    if (bundle == null)
        return;

    String state = bundle.getString(TelephonyManager.EXTRA_STATE);

    if ((state != null) && 
            (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))) {
        Log.i("TAG", "incoming call");

        Uri contactUri = intent.getData();
        String[] projection = { Phone.DISPLAY_NAME };
        //i cannot use getContentResolver()
        Cursor cursor = getContentResolver()..query(contactUri, projection, null, 
                                                         null, null);

        int columnName = cursor.getColumnIndex(Phone.DISPLAY_NAME);
        String contactName = cursor.getString(columnName);

        m_number = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);

        editor.putString(PREFS_NUMBER, m_number);
        editor.commit();

    } else if(state == null) {
        Log.i("TAG", "outgoing call");

        Uri contactUri = intent.getData();
        String[] projection = { Phone.DISPLAY_NAME };
        //i cannot use getContentResolver()
        Cursor cursor = getContentResolver()..query(contactUri, projection, null, 
                                                        null, null);

        int columnName = cursor.getColumnIndex(Phone.DISPLAY_NAME);
        String contactName = cursor.getString(columnName);

        m_number = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);

        editor.putString(PREFS_NUMBER, m_number);
        editor.commit();

    } else if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
        Log.i("TAG", "off hook");

        Time dtstart = new Time(Time.getCurrentTimezone());
        dtstart.setToNow();

        m_startTime = dtstart.format("%k:%M:%S");

        editor.putString(PREFS_START_TIME, m_startTime);
        editor.commit();

    } else if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)) {
        Log.i("TAG", "on idle");

        Time dtend = new Time(Time.getCurrentTimezone());
        dtend.setToNow();

        m_endTime = dtend.format("%k:%M:%S");

        editor.putString(PREFS_END_TIME, m_endTime);
        editor.commit();
    }


    }

this is the service class:

    public class TrackerService extends Service {
    PhoneStateBroadcastReciever receiver;

    @Override
    public void onCreate() {
    receiver = new PhoneStateBroadcastReciever();

    IntentFilter filter = new IntentFilter();
    filter.addAction(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED);
    registerReceiver(receiver, filter);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "starting service", Toast.LENGTH_SHORT).show();
    return Service.START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public void onDestroy() {
    unregisterReceiver(receiver);
    Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
    }
    }
4

1 回答 1

0

使用Context调用contentResolver()。像这样的东西:

context.getContentResolver()....
于 2013-10-29T10:53:32.113 回答