How to read SMS and Call logs into stock ROM android


This is a test application,I built this application to understanding the operation of storing SMS and call logs.

here the complete project: Download PRJ
This task is very simple,the work is all done by this asynctask which derives the information:

protected class ReadTask extends AsyncTask<Integer, String, String> {
	ProgressDialog dialog = null;
	Context context = null;

	public ReadTask(Context context) {
		this.context = context;

		dialog = ProgressDialog.show(context, "",
				getString(R.string.str_generic_progressbar), true);
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
	}

	@Override
	protected String doInBackground(Integer... params) {

		StringBuilder stbT = new StringBuilder();

		switch (params[0]) {
		case TYPE_SMS:
			try {

				Uri SMSURI = Uri.parse("content://sms/");
				String[] projection = new String[] { "_id", "address",
						"body", "date" };
				Cursor cursor = null;
				try {
					cursor = context.getContentResolver().query(SMSURI,
							projection, null, null, null);
					if (cursor != null && cursor.moveToFirst()) {

						do {

							int id = cursor.getInt(cursor
									.getColumnIndex("_id"));
							String address = cursor.getString(cursor
									.getColumnIndex("address"));
							String body = cursor.getString(cursor
									.getColumnIndex("body"));
							String date = cursor.getString(cursor
									.getColumnIndex("date"));
							SimpleDateFormat formatter = new SimpleDateFormat(
									"dd/MM/yyyy - HH:mm:ss");
							date = formatter.format(new Date(Long
									.parseLong(date)));

							stbT.append("\n\nNew SMS\n-----\nAndress:"
									+ address + "\nMessage:" + body
									+ "\nDate:" + date);

						} while (cursor.moveToNext());
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					if (cursor != null) {
						cursor.close();
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;

		case TYPE_CALL:
			try {
				long dialed;
				String columns[] = new String[] { CallLog.Calls._ID,
						CallLog.Calls.NUMBER, CallLog.Calls.DATE,
						CallLog.Calls.DURATION, CallLog.Calls.TYPE };
				Cursor c;

				String strOrder = android.provider.CallLog.Calls.DATE
						+ " DESC";
				c = getContentResolver().query(
						Uri.parse("content://call_log/calls"), columns,
						null, null, strOrder); // last record first
				while (c.moveToNext()) {
					dialed = c
							.getLong(c.getColumnIndex(CallLog.Calls.DATE));

					stbT.append("\n\nNew Call\n-----\nType:"
							+ c.getString(c
									.getColumnIndex(CallLog.Calls.TYPE))
							+ "\nNumber:"
							+ c.getString(c
									.getColumnIndex(CallLog.Calls.NUMBER))
							+ "\nDate:" + new Date(dialed).toString());
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		}
		return stbT.toString();
	}

	@Override
	protected void onProgressUpdate(String... values) {
		super.onProgressUpdate(values);

	}

	@Override
	protected void onPostExecute(String result) {
		super.onPostExecute(result);

		tvOut.setText(result);

		dialog.dismiss();
	}
}

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...