Android读取手机短信

    xiaoxiao2022-07-12  136

     

    package com.weituo.messagereaddemo; import android.Manifest; import android.content.pm.PackageManager; import android.database.Cursor; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.RequiresApi; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.widget.TextView; import java.text.SimpleDateFormat; import java.util.Date; public class MainActivity extends AppCompatActivity { private TextView tvMessage; private String address = "18665651567"; @RequiresApi(api = Build.VERSION_CODES.M) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvMessage = findViewById(R.id.tv_message); //申请写的权限 String[] permissions = {Manifest.permission.READ_SMS}; if(PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)){ requestPermissions(permissions,200); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if(!TextUtils.isEmpty(getSMS())){ tvMessage.setText(getSMS()); } } private String getSMS() { final String SMS_URI_INBOX = "content://sms/inbox"; Uri uri = Uri.parse(SMS_URI_INBOX); String[] projection = new String[]{"_id", "address", "person", "body", "date", "type",}; // Cursor cursor = getContentResolver().query(uri, projection, null, null, "date desc"); Cursor cur = getContentResolver().query(uri, projection, "read = ?", new String[]{"0"}, "date desc"); StringBuilder smsBuilder = new StringBuilder(); if(cur.moveToFirst()){ int index_Address = cur.getColumnIndex("address"); int index_Person = cur.getColumnIndex("person"); int index_Body = cur.getColumnIndex("body"); int index_Date = cur.getColumnIndex("date"); int index_Type = cur.getColumnIndex("type"); do { String strAddress = cur.getString(index_Address); int intPerson = cur.getInt(index_Person); String strbody = cur.getString(index_Body); long longDate = cur.getLong(index_Date); int intType = cur.getInt(index_Type); SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss"); Date d = new Date(longDate); String strDate = dateFormat.format(d); String strType = ""; if (intType == 1) { strType = "接收"; } else if (intType == 2) { strType = "发送"; } else if (intType == 3) { strType = "草稿"; } else if (intType == 4) { strType = "发件箱"; } else if (intType == 5) { strType = "发送失败"; } else if (intType == 6) { strType = "待发送列表"; } else if (intType == 0) { strType = "所以短信"; } else { strType = "null"; } if(strAddress.equals(address)){ smsBuilder.append("[ "); smsBuilder.append(strAddress + ", "); smsBuilder.append(intPerson + ", "); smsBuilder.append(strbody + ", "); smsBuilder.append(strDate + ", "); smsBuilder.append(strType); smsBuilder.append(" ]\n\n"); } } while (cur.moveToNext()); if (!cur.isClosed()) { cur.close(); cur = null; } }else{ smsBuilder.append("no result!"); } smsBuilder.append("getSmsInPhone has executed!"); return smsBuilder.toString(); } }

     

     

     

    最新回复(0)