smile908

导航

安卓读取手机短信

上次接了一个项目,大致意思是一个页面,有六个输入(EditText),以及两个Button,一个button用于

读取短信,并处理读取的信息填充至六个EditText里面,另外一个按钮用于清除掉六个EditText里面的内容。

下面是运行效果图:

 

 首先,我们看manifest.xml文件,在这里我们需要添加两个关于读取短信的权限。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_SMS"/>
当然只加这个权限还是会报错。大致的意思是说权限被拒绝。
后来我在oncreate()方法里面加上了动态申请权限,问题解决。
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1){
// requestPermission();
requestPermissions(new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.READ_SMS },
R.string.app_name);

//this code will be executed on devices running ICS or later
}

ui部分不用看了,很简单的代码,六个TextView,六个EditText,两个button,一个ImageView就组成了这个页面。

MainActivity.java页面
package com.example.movielibrary;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private Button clearAll;
private EditText ev_title;
private EditText ev_year;
private EditText ev_country;
private EditText ev_genre;
private EditText ev_cost;
private EditText ev_keywords;
private String text;
private Button add;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1){
// requestPermission();
requestPermissions(new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.READ_SMS },
R.string.app_name);

//this code will be executed on devices running ICS or later
}
init();
}

private void init(){
clearAll = findViewById(R.id.btn_clear);
add = findViewById(R.id.btn_add);

ev_title = findViewById(R.id.ev_title);
ev_year = findViewById(R.id.ev_year);
ev_country = findViewById(R.id.ev_country);
ev_genre = findViewById(R.id.ev_genre);
ev_cost = findViewById(R.id.ev_cost);
ev_keywords = findViewById(R.id.ev_keywords);
clearAll.setOnClickListener(this);
add.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_clear:
clearAll();
break;
case R.id.btn_add:
readSms();
break;
}
}

private void clearAll() {
isEmpty(ev_title);
isEmpty(ev_year);
isEmpty(ev_country);
isEmpty(ev_genre);
isEmpty(ev_cost);
isEmpty(ev_keywords);
}

private void isEmpty(EditText et){
if (et.getText()!=null||et.getText().toString()!=""){
et.setText("");
}
}


public void readSms(){
//查询短信的uri
Uri uri = Uri.parse("content://sms/");
//获取ContentResolver对象
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(uri,new String[]{
"_id","address","type","body","date"},null,null,null);
List<SmsInfo> smsInfos = new ArrayList<SmsInfo>();
if (cursor != null && cursor.getCount()>0){
while(cursor.moveToNext()){
int _id = cursor.getInt(0);
String address =cursor.getString(1);
int type = cursor.getInt(2);
String body = cursor.getString(3);
long date = cursor.getLong(4);
SmsInfo smsInfo = new SmsInfo(_id,address,type,body,date);
smsInfos.add(smsInfo);
}
cursor.close();
}
for (int i=0;i<smsInfos.size();i++){
text =smsInfos.get(0).getBody();
//获取一条
if (text==null){
return;
}
if (text.contains(",")){
String[] split = text.split(",");
ev_title.setText(split[0]);
ev_year.setText(split[1]);
ev_country.setText(split[2]);
ev_genre.setText(split[3]);
ev_cost.setText(split[4]);
ev_keywords.setText(split[5]);
}else if (text.contains(",")){
String[] split = text.split(",");
ev_title.setText(split[0]);
ev_year.setText(split[1]);
ev_country.setText(split[2]);
ev_genre.setText(split[3]);
ev_cost.setText(split[4]);
ev_keywords.setText(split[5]);
}

}
}
}
这样就成能够成功运行了,点击add的Button,就能从短信中获取文字并处理填写到EditText中,点击Clear按钮内容将会被清空。这样,
我们需要的功能就这样实现了。

posted on 2022-11-23 17:31  smile908  阅读(1399)  评论(0编辑  收藏  举报