Android将发送的短信插入数据库
16lz
2022-05-25
注意点:在Android 5.1.1 Android 9.0.1 型号的机型不行,该功能实现的机型:Android 6.1.1
package com.cari.demo;import android.app.Activity;import android.content.ContentProvider;import android.content.ContentValues;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.widget.EditText;import android.widget.Toast;import org.apache.log4j.Logger;public class PageActivity extends Activity { private Logger logger; private EditText number,body; private String AA,BB; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_page); logger=Logger.getLogger(PageActivity.class); number=(EditText)findViewById(R.id.number); body=(EditText)findViewById(R.id.body); } // 把短信写入数据库 public void writeMsg(String aa,String bb){ try{ ContentValues values = new ContentValues(); // 发送时间 values.put("date", System.currentTimeMillis()); // 阅读状态 values.put("read", 0); // 类型:1为收,2为发 values.put("type", 2); // 发送号码 values.put("address",aa); // 发送内容 values.put("body", bb); // 插入短信库 getContentResolver().insert(Uri.parse("content://sms/sent"), values); logger.info("values===="+values); }catch (Exception e) { Log.d("Exception", e.getMessage()); } } public void insertSMStoDB(String aa,String bb){ try{ logger.info("方法二===="); ContentValues va=new ContentValues(); va.put("date",System.currentTimeMillis()); //阅读状态 va.put("read",0); //1为收 2为发 va.put("type",2); //送达号码 va.put("address",aa); va.put("body",bb); logger.info("va===="+va); getContentResolver().insert(Uri.parse("content://sms"),va); }catch (Exception e) { Log.d("Exception", e.getMessage()); } } public void sendApp(View view){ if (TextUtils.isEmpty(number.getText().toString())||TextUtils.isEmpty(body.getText().toString())){ Toast.makeText(PageActivity.this,"账户或密码不能为空",Toast.LENGTH_SHORT).show(); }else{ AA=number.getText().toString(); BB=body.getText().toString(); logger.info("发送消息成功===="); writeMsg(AA,BB);// insertSMStoDB(AA,BB); } }}
<?xml version="1.0" encoding="utf-8"?>
更多相关文章
- Android下修改SeekBar样式
- android 把图片状态由彩色变成灰色
- Android腾讯微博客户端开发四:微博发送篇(QQ表情,@搜索)
- android 发送短信sms
- android canvas中的save()和restore()的作用及效果
- android状态栏 高度
- Android(安卓)底部导航BottomNavigationView(非官方)
- Android根据电话号码取得联系人姓名及头像
- Android状态栏完全透明和改变状态栏颜色实现