欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

android读取短信示例分享

时间:2021-07-28 07:42:41|栏目:Android代码|点击:

复制代码 代码如下:

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自定义View构造函数详解

栏    目:Android代码

下一篇:简单好用的Adapter---ArrayAdapter详解

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

本文地址:http://www.codeinn.net/misctech/162117.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有