移动应用开发程序演练——ContentProvider(内容提供者)

工具:Android Studio

查看短信的猫(内容提供者的创建和查看)

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:orientation="vertical"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg"/>
    <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
        android:id="@+id/tv_sms"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_des"
        android:lines="20"
        android:paddingLeft="20dp"
        android:paddingTop="10dp"
        android:textSize="16sp"/>
    <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="#D9D1FA"
        android:onClick="readSMS"
        android:text="查看短信"
        android:textSize="30sp"/>

</RelativeLayout>

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;
    }
}
 

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.view.View; import
> android.widget.TextView; import java.util.ArrayList; import
> java.util.List;
> 
> import android.os.Bundle;
> 
> 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
  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一角灯辉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值