0

我正在尝试从通话记录中获取最近未接的电话号码。我使用了下面的代码,我想让它在后台运行,现在我把它写成我的 mainActivity 类中的一个方法,当一个呼叫移动到未接来电日志时,这个函数应该可以工作。如何使它成为可能请帮助

  public void SendSMStoMissedCall()
{
    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));
                number = mCallCursor.getString(mCallCursor.getColumnIndex(CallLog.Calls.NUMBER));
                String time = mCallCursor.getString(mCallCursor.getColumnIndex(android.provider.CallLog.Calls.DATE));
                      Log.d("PhoneLog", "You have a missed call from " + name + " on " + number  + " at " + time);  
                String ContactName = objUtility.getContactNameFromNumber(number);

                SendMessage(number);
                getCurrentLocation();
            }
       } 
      while (mCallCursor.moveToNext());

     }

}
4

1 回答 1

0

创建一个服务,PhoneStateListener在服务中实现一个。在扩展类中PhoneStateListener,在onCallStateChanged方法中把你的代码放在上面的TelephonyManager.CALL_STATE_IDLEcase中。

于 2013-03-22T15:58:32.707 回答