Android携帯電話のすべての短い情報を読み取る
9284 ワード
import android.app.Activity;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import com.feilu.flashloan.callback.OnSmsInboxListener;
import com.feilu.flashloan.ui.usercenter.bean.SmsInboxBean;
import com.google.gson.Gson;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Created by fengpeihao on 2017/8/15.
*
*/
public class SmsInboxUtils {
private static SmsInboxUtils mContactUtils;
private OnSmsInboxListener mListener;
public static SmsInboxUtils with() {
if (mContactUtils == null) {
synchronized (ContactUtils.class) {
mContactUtils = new SmsInboxUtils();
}
}
return mContactUtils;
}
public void getSms(Activity activity, OnSmsInboxListener listener) {
mListener = listener;
MyAsyncQueryhandler asyncQueryhandler = new MyAsyncQueryhandler(activity.getContentResolver());
/**
* content://sms/
* content://sms/inbox
* content://sms/sent
* content://sms/draft
* content://sms/outbox
* content://sms/failed
* content://sms/queued
*/
Uri uri = Uri.parse("content://sms/");
String[] projection = new String[]{"address", "person", "body", "date", "type"};//"_id", "address", "person","date", "type"1 ,2
asyncQueryhandler.startQuery(0, null, uri, projection, null, null, "date desc");
}
private String getDate(String date) {
Date callDate = new Date(Long.parseLong(date));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(callDate);
}
private class MyAsyncQueryhandler extends AsyncQueryHandler {
public MyAsyncQueryhandler(ContentResolver cr) {
super(cr);
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
if (cursor != null && cursor.getCount() > 0) {
List list = new ArrayList<>();
while (cursor.moveToNext()) {
String number = cursor.getString(cursor.getColumnIndex("address"));//
String name = cursor.getString(cursor.getColumnIndex("person"));//
String body = cursor.getString(cursor.getColumnIndex("body"));//
String date = getDate(cursor.getString(cursor.getColumnIndex("date")));//
String type = cursor.getString(cursor.getColumnIndex("type"));//1 ,2
SmsInboxBean smsInboxBean = new SmsInboxBean(date, name, number, body, type);
list.add(smsInboxBean);
}
Gson gson = new Gson();
mListener.onSuccess(gson.toJson(list));
} else {
if (mListener != null) mListener.onFailed();
}
super.onQueryComplete(token, cookie, cursor);
}
}
}
public interface OnSmsInboxListener { void onSuccess(String json); void onFailed(); }
:
<uses-permission android:name="android.permission.READ_SMS"/>
6.0