讀取短信內容
讀取短信的內容就要知道短信放在哪裏,然後通過查詢語句把結果封裝到實體類的List中,再全部打印出來即可。
下面是具體的代碼:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_des"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:paddingLeft="20dp"
android:text="讀取到的系統短信如下:"
android:textSize="20sp"
android:visibility="invisible">
</TextView>
<TextView
android:id="@+id/tv_sms"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_des"
android:layout_marginTop="0dp"
android:lines="20"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:textSize="16sp">
</TextView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="28dp"
android:layout_marginLeft="26dp"
android:layout_marginStart="26dp"
android:background="#149EDD"
android:onClick="readSms"
android:padding="5dp"
android:text="查看短信"
android:textSize="30sp">
</Button>
</RelativeLayout>
MainActivity.java
package com.example.readsms;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.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) {
super.onCreate(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();
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){
tvDes.setVisibility(View.VISIBLE);
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(i).getAddress()+"\n";
text += "短信內容" + smsInfos.get(i).getBody()+"\n";
tvSms.setText(text);
}
}
}
SmsInfo.java
package com.example.readsms;
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.type = type;
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) {
this.type = 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 +
'}';
}
}
Androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.readsms">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
運行截圖:
與短信內容一致。