1. 使用ContentResolver获取手机短信信息
代码:
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_SMS_PERMISSION = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = findViewById(R.id.layout);
ListView listView = findViewById(R.id.listview);
findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String SMS_URI_ALL = "content://sms/"; //所有短信
final String SMS_URI_INBOX = "content://sms/inbox"; //收信箱
final String SMS_URI_SEND = "content://sms/sent"; //发信箱
final String SMS_URI_DRAFT = "content://sms/draft"; //草稿箱
String[] items = new String[]{
"_id", "address", "person", "body", "date", "type"};
Uri mUri = Uri.parse(SMS_URI_INBOX);
ArrayList<String> arrayList = new ArrayList<>();
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(mUri,items,null,null
,"date desc");
if(cursor!=null){
while(cursor.moveToNext()){
String phone = cursor.getString(cursor.getColumnIndex("address"));
String body = cursor.getString(cursor.getColumnIndex("body"));
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date tempDate = new Date(Long.p