android-如何解析短信textview

时间:2019-06-26 22:00:28

标签: java android textview sms

我正在尝试获取短信并解析textView。我的代码是什么错误?

并且ı测试了此代码,但无法正常工作 此代码在哪里出错,我想获取短信并解析textview

public class ReadMsg extends AppCompatActivity {

ListView listView;
private static final int PERMISSION_REQUEST_READ_CONTACTS = 100;
ArrayList smsList;
TextView textsms;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_read_msg);
    listView = (ListView)findViewById(R.id.idList);
textsms = findViewById(R.id.textsms);


    int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS);

    if (permissionCheck == PackageManager.PERMISSION_GRANTED){
        showContacts();
    }else{
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS},PERMISSION_REQUEST_READ_CONTACTS);
    }
}

private void showContacts() {
    Uri inboxURI = Uri.parse("content://sms/inbox");
    smsList = new ArrayList();
    ContentResolver cr = getContentResolver();


    Cursor c = cr.query(inboxURI,null,null,null,null);
    while (c.moveToNext()){
        String Number = c.getString(c.getColumnIndexOrThrow("address")).toString();
        String Body= c.getString(c.getColumnIndexOrThrow("body")).toString();
        smsList.add("Number: "+ Number + "\n" + "Body: "+ Body );
        textsms.setText(body);
    }
        c.close();
    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, smsList);
    listView.setAdapter(adapter);
}

0 个答案:

没有答案