我试图从通话记录中取出最近错过的电话号码。我使用下面的代码我想让它在后台运行,现在我把它作为一个方法写在我的mainActivity类中,当一个调用移动到未接来电日志时,这个功能应该有效。如何使plz帮助成为可能
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());
}
}
答案 0 :(得分:0)
创建服务,在服务中实施PhoneStateListener
。在PhoneStateListener
的扩展类中,在onCallStateChanged
方法中将您的代码置于TelephonyManager.CALL_STATE_IDLE
案例中。