[android] 插入一条记录到系统短信应用里

谷歌市场上有这些应用,模拟短信,原理就是把数据插入到短信应用的数据库里

获取ContentResolver对象,通过getContentResolver()方法

调用resolver对象的insert(uri,values)方法,参数:Uri对象,ContentValues对象

调用ContentValues对象的put(key,value)方法,key就是上一节的字段,val值,date是时间戳,调用系统的时间System.currentTimeMillies()方法

使用线程来实现过几秒后再运行这段程序,直接new Thread的匿名内部类,类里面重写run()方法,得到的对象调用start()方法,开启新线程,调用Thread类的sleep()方法睡眠一段时间再往下运行。

这里清单文件中要读写短信的权限

activity:

package com.tsh.makesms;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //开启线程,延迟20秒
        new Thread(){
            public void run(){
                try {
                    Thread.sleep(20000);
                    ContentResolver resolver=getContentResolver();
                    Uri url=Uri.parse("content://sms/");
                    ContentValues values=new ContentValues();
                    values.put("address", "10086");
                    values.put("type", 1);
                    values.put("date", System.currentTimeMillis());
                    values.put("body", "我是10086的,快开门");
                    resolver.insert(url, values);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                
            }
        }.start();

    }

}
原文地址:https://www.cnblogs.com/taoshihan/p/5272692.html