在我的项目中,我检索了我的通话记录和短信数据,并希望它在后台通过电子邮件发送。检索到的数据代码是:-(在 WakefulBroadcastReceiver 中)
private Context mContext;
@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
getSMSData();
getCallLogData();
}
private void getSMSData() {
ContentResolver contentResolver = mContext.getContentResolver();
// use content://sms/inbox/ for received and content://sms/sent/ for sent messages
Uri uri = Uri.parse("content://sms/");
String selection = "date BETWEEN ? AND ? ";
long currentTime = System.currentTimeMillis();
long pastThreeHour = currentTime - (AlarmManager.INTERVAL_HOUR * 3);
String[] selectionArgs = { "" + pastThreeHour, "" + currentTime };
Cursor cursor = contentResolver.query(uri, null, selection, selectionArgs, "date DESC");
if (cursor != null && cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String number = cursor.getString(cursor.getColumnIndex("address")); // check for null
String date = cursor.getString(cursor.getColumnIndex("date")); // convert to date its long
String message_text = cursor.getString(cursor.getColumnIndex("body"));
String type = cursor.getString(cursor.getColumnIndex("type")); // check type and get names
// send email from here
sendSMSEmail(number, date, message_text, type);
}
}
cursor.close();
}
private void getCallLogData() {
Uri uri = Uri.parse("content://call_log/calls");
String[] projection = new String[] { Calls.TYPE, Calls.NUMBER, Calls.DATE, Calls.CACHED_NAME, Calls.DURATION };
String selection = "date BETWEEN ? AND ? ";
long currentTime = System.currentTimeMillis();
long pastThreeHour = currentTime - (AlarmManager.INTERVAL_HOUR * 3);
String[] selectionArgs = { "" + pastThreeHour, "" + currentTime };
Cursor cursor = mContext.getContentResolver().query(uri, projection, selection, selectionArgs, "date DESC");
if (cursor != null && cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String num = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
// convert long date to date
String date = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.DATE));
String name = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));
String duration = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.DURATION));
int type = Integer.parseInt(cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.TYPE)));
String typeName = null;
switch (type) {
case 1:
typeName = "Incoming";
break;
case 2:
typeName = "Outgoing";
break;
case 3:
typeName = "Missed";
break;
}
sendCallEmail(num, date, name, duration, typeName);
}
}
}
private void sendCallEmail(String num, String date, String name, String duration, String typeName) {
// TODO Auto-generated method stub
}
private void sendSMSEmail(String number, String date, String message_text, String type) {
// TODO Auto-generated method stub
}
}
实际上我不明白如何将这些检索到的数据发送到我的邮件 ID。邮件 ID 只能由用户提供。请帮我发邮件。提前致谢。