zoukankan      html  css  js  c++  java
  • 短信获取

    <?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>
    

      

    package com.example.duanxin;
    
    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", "短信插入完毕");
        }
    }
    

      

  • 相关阅读:
    熊逸吴军武志红万维钢薛兆丰等得到专栏书34本,5星1本,4星12本
    2星|罗大伦《道德经说什么》:比熊逸《道可道》李零《人往低处走》差很多
    樊登力荐的《道德经说什么》,比熊逸《道可道》差两颗星
    Mysql授权允许远程访问解决Navicat for MySQL连接mysql提示客户端不支持服务器请求的身份验证协议;考虑升级MySQL客户端
    使用Vue-cli 脚手架生成的项目使用Vetur+ Prettier + ESlint的配置设置代码规范和格式
    URL中的hash(井号)
    Redis集群的部署
    Redis用作分布式锁
    Redis 概述安装
    简单的Asp.net core管道模拟
  • 原文地址:https://www.cnblogs.com/mhlhaoshuai/p/14169545.html
Copyright © 2011-2022 走看看