Android读取短信内容(模拟器)
读取短信内容
读取短信的内容就要知道短信放在哪⾥,然后通过查询语句把结果封装到实体类的List中,再全部打印出来即可。下⾯是具体的代码:
l
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android="schemas.android/apk/res/android"
app="schemas.android/apk/res-auto"
tools="schemas.android/tools"
layout_width="match_parent"
layout_height="match_parent"
background="@drawable/bg"
orientation="vertical"
context=".MainActivity">
<TextView
id="@+id/tv_des"
layout_width="match_parent"
layout_height="wrap_content"
layout_marginTop="18dp"
paddingLeft="20dp"
text="读取到的系统短信如下:"
textSize="20sp"
visibility="invisible">
</TextView>
<TextView
id="@+id/tv_sms"
layout_width="match_parent"
layout_height="wrap_content"
layout_below="@id/tv_des"
layout_marginTop="0dp"
lines="20"
paddingLeft="20dp"
paddingTop="10dp"
textSize="16sp">
</TextView>
<Button
layout_width="wrap_content"
layout_height="wrap_content"
layout_alignParentBottom="true"
layout_alignParentLeft="true"
layout_alignParentStart="true"
layout_marginBottom="28dp"
layout_marginLeft="26dp"
layout_marginStart="26dp"
background="#149EDD"
onClick="readSms"
padding="5dp"
text="查看短信"
textSize="30sp">
</Button>
</RelativeLayout>
MainActivity.java
adsms;
import androidx.appcompat.app.AppCompatActivity;
t.ContentResolver;
import android.database.Cursor;
import android.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TextView tvSms;
private TextView tvDes;
private String text;
@Override
protected void onCreate(Bundle savedInstanceState){
setContentView(R.layout.activity_main);
tvSms =(TextView)findViewById(R.id.tv_sms);
tvDes =(TextView)findViewById(R.id.tv_des);
}
public void readSms(View view){
//查询短信的uri
Uri uri = Uri.parse("content://sms/");
//获取ContentResolver对象
ContentResolver resolver =getContentResolver();android模拟点击
Cursor cursor = resolver.query(uri,new String[]{
"_id","address","type","body","date"},null,null,null);
List<SmsInfo> smsInfos =new ArrayList<SmsInfo>();
if(cursor != null && Count()>0){
tvDes.setVisibility(View.VISIBLE);
veToNext()){
int _id = Int(0);
String address =String(1);
int type = Int(2);
String body = String(3);
long date = Long(4);
SmsInfo smsInfo =new SmsInfo(_id,address,type,body,date); smsInfos.add(smsInfo);
}
cursor.close();
}
for(int i=0;i<smsInfos.size();i++){
text +="⼿机号码"+ (i).getAddress()+"\n";
text +="短信内容"+ (i).getBody()+"\n";
tvSms.setText(text);
}
}
}
SmsInfo.java
adsms;
public class SmsInfo {
private int _id;//短信主键
private String address;//发送地址
private int type;//类型
private String body;//短信内容
private long date;//时间
public SmsInfo(int _id, String address,int type, String body,long date){ this._id = _id;
this.address = address;
this.body = body;
this.date = date;
}
public int get_id(){
return _id;
}
public void set_id(int _id){
this._id = _id;
}
public String getAddress(){
return address;
}
public void setAddress(String address){
this.address = address;
}
public int getType(){
return type;
}
public void setType(int type){
}
public String getBody(){
return body;
}
public void setBody(String body){
this.body = body;
}
public long getDate(){
return date;
}
public void setDate(long date){
this.date = date;
}
@Override
public String toString(){
return"SmsInfo{"+
"_id="+ _id +
", address='"+ address +'\''+
", type="+ type +
", body='"+ body +'\''+
", date="+ date +
'}';
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest android="schemas.android/apk/res/android"
package="adsms">
<uses-permission name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission> <uses-permission name="android.permission.READ_SMS"></uses-permission>
<application
allowBackup="true"
icon="@mipmap/ic_launcher"
label="@string/app_name"
roundIcon="@mipmap/ic_launcher_round"
supportsRtl="true"
theme="@style/AppTheme">
<activity name=".MainActivity">
<intent-filter>
<action name="android.intent.action.MAIN"/>
<category name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
运⾏截图:
与短信内容⼀致。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论