zoukankan      html  css  js  c++  java
  • Android第十一周作业

    内容提供者读取短信信息

    package com.example.myapplication;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.ContentResolver;
    import android.content.ContentValues;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button button1 = (Button)findViewById(R.id.button);
            Button button2 = (Button)findViewById(R.id.button2);
    
    
            button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getMsgs();
                }
            });
    
            button2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    insertMsg();
                }
            });
    
    
        }
    
        private void getMsgs(){
            Uri uri = Uri.parse("content://sms/");
            ContentResolver resolver = getContentResolver();
            //获取的是哪些列的信息
            Cursor cursor = resolver.query(uri, new String[]{"address","date","type","body"}, null, null, null);
            while(cursor.moveToNext())
            {
                String address = cursor.getString(0);
                String date = cursor.getString(1);
                String type = cursor.getString(2);
                String body = cursor.getString(3);
                System.out.println("地址:" + address);
                System.out.println("时间:" + date);
                System.out.println("类型:" + type);
                System.out.println("内容:" + body);
                System.out.println("======================");
            }
            cursor.close();
        }
    
    
        private void insertMsg() {
            ContentResolver resolver = getContentResolver();
            Uri uri = Uri.parse("content://sms/");
            ContentValues conValues = new ContentValues();
            conValues.put("address", "123456789");
            conValues.put("type", 1);
            conValues.put("date", System.currentTimeMillis());
            conValues.put("body", "no zuo no die why you try!");
            resolver.insert(uri, conValues);
            Log.e("HeHe", "短信插入完毕~");
        }
    }
    

      

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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" >
    
        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="读取" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="插入" />
    </LinearLayout>
    

      

     

  • 相关阅读:
    Oracle 删表前验证表名是否存在并且删除
    Mysql的建表规范与注意事项
    MYSQL总结之sql语句大全
    主机屋云服务器(绑定域名)初探
    (十)Thymeleaf用法——Themeleaf内联
    (九)Thymeleaf用法——Themeleaf注释
    (八)Thymeleaf的 th:* 属性之—— 模板布局& th:with& 属性优先级
    (七)Thymeleaf的 th:* 属性之—— th: ->设值& 遍历迭代& 条件判断
    (六)Thymeleaf的 th:* 属性之—— th: ->text& utext& href
    (五)Thymeleaf标准表达式之——[7->8]条件表达式& 默认表达式
  • 原文地址:https://www.cnblogs.com/a000/p/14164219.html
Copyright © 2011-2022 走看看