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"); // 獲取手機(jī)內(nèi)部短信
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 = "發(fā)送";
} 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" />
相關(guān)文章
Android判斷是Wifi還是4G網(wǎng)絡(luò)代碼
這篇文章主要為大家詳細(xì)介紹了Android判斷網(wǎng)絡(luò)類型的方法,判斷是Wifi還是4G網(wǎng)絡(luò)代碼分享,感興趣的小伙伴們可以參考一下2016-07-07android 獲取手機(jī)內(nèi)存及 內(nèi)存可用空間的方法
下面小編就為大家?guī)硪黄猘ndroid 獲取手機(jī)內(nèi)存及SD卡內(nèi)存可用空間的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03Android ListView滾動到底后自動加載數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了Android之ListView滾動到底后自動加載數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09Textvie實現(xiàn)左邊圖片和換行文字左對齊的方法
下面小編就為大家分享一篇Textvie實現(xiàn)左邊圖片和換行文字左對齊的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01Android開發(fā)實現(xiàn)橫向列表GridView橫向滾動的方法【附源碼下載】
這篇文章主要介紹了Android開發(fā)實現(xiàn)橫向列表GridView橫向滾動的方法,結(jié)合實例形式分析了Android橫向列表GridView實現(xiàn)橫向滾動的相關(guān)布局與功能實現(xiàn)技巧,并附帶源碼供讀者下載參考,需要的朋友可以參考下2018-01-01深入android中The connection to adb is down的問題以及解決方法
本篇文章是對android中The connection to adb is down的問題以及解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05