返回顶部
首页 > 资讯 > 移动开发 >android读取短信示例分享
  • 283
分享到

android读取短信示例分享

示例Android 2022-06-06 10:06:14 283人浏览 独家记忆
摘要

代码如下:package com.homer.sms; import java.sql.Date;import java.text.SimpleDateFORMat; imp

代码如下:
package com.homer.sms;

import java.sql.Date;
import java.text.SimpleDateFORMat;


import Android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TextView;
public class smsRead extends Activity {

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  TextView tv = new TextView(this);
  tv.setText(getSmsInPhone());

  ScrollView sv = new ScrollView(this);
  sv.addView(tv);
  setContentView(sv);
 }

 public String getSmsInPhone() {
  final String SMS_URI_ALL = "content://sms/";
  final String SMS_URI_INBOX = "content://sms/inbox";
  final String SMS_URI_SEND = "content://sms/sent";
  final String SMS_URI_DRAFT = "content://sms/draft";
  final String SMS_URI_OUTBOX = "content://sms/outbox";
  final String SMS_URI_FaiLED = "content://sms/failed";
  final String SMS_URI_QUEUED = "content://sms/queued";

  StringBuilder smsBuilder = new StringBuilder();

  try {
   Uri uri = Uri.parse(SMS_URI_ALL);
   String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };
   Cursor cur = getContentResolver().query(uri, projection, null, null, "date desc");  // 获取手机内部短信

   if (cur.moveToFirst()) {
    int index_Address = cur.getColumnIndex("address");
    int index_Person = cur.getColumnIndex("person");
    int index_Body = cur.getColumnIndex("body");
    int index_Date = cur.getColumnIndex("date");
    int index_Type = cur.getColumnIndex("type");

    do {
     String strAddress = cur.getString(index_Address);
     int intPerson = cur.getInt(index_Person);
     String strbody = cur.getString(index_Body);
     long longDate = cur.getLong(index_Date);
     int intType = cur.getInt(index_Type);

     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
     Date d = new Date(longDate);
     String strDate = dateFormat.format(d);

     String strType = "";
     if (intType == 1) {
      strType = "接收";
     } else if (intType == 2) {
      strType = "发送";
     } else {
      strType = "null";
     }

     smsBuilder.append("[ ");
     smsBuilder.append(strAddress + ", ");
     smsBuilder.append(intPerson + ", ");
     smsBuilder.append(strbody + ", ");
     smsBuilder.append(strDate + ", ");
     smsBuilder.append(strType);
     smsBuilder.append(" ]\n\n");
    } while (cur.moveToNext());

    if (!cur.isClosed()) {
     cur.close();
     cur = null;
    }
   } else {
    smsBuilder.append("no result!");
   } // end if

   smsBuilder.append("getSmsInPhone has executed!");

  } catch (SQLiteException ex) {
   Log.d("SQLiteException in getSmsInPhone", ex.getMessage());
  }

  return smsBuilder.toString();
 }
}

记得在AndroidManifest.xml中加入android.permission.READ_SMS这个permission
代码如下:
<uses-permission android:name="android.permission.READ_SMS" />

您可能感兴趣的文章:android短信拦截的实现代码Android短信接收监听、自动回复短信操作例子Android实现获取未接来电和未读短信数量的方法Android发送短信功能代码android短信监听工具(示例代码)Android实现发送短信功能实例详解android中可以通过两种方式调用接口发送短信Android获取和读取短信验证码的实现方法Android实现短信验证码自动填写功能Android实现自动提取短信验证码功能Android编程实现拦截短信并屏蔽系统Notification的方法Android编程实现定时发短信功能示例


--结束END--

本文标题: android读取短信示例分享

本文链接: https://lsjlt.com/news/27165.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作