Android開發(fā)獲取短信的內(nèi)容并截取短信
1、首先我們要寫一個(gè)廣播接收器,當(dāng)我們的手機(jī)收到短信時(shí),系統(tǒng)會(huì)自動(dòng)發(fā)送一個(gè)廣播,我們只需要接收到這條廣播就可以了
2、在廣播里面,我們重寫的onReceive()方法,通過(guò)里面的Intent寫到的Bundle就可以拿到短信的內(nèi)容,
3、清單文件里面我們必須要添加權(quán)限,否則無(wú)法接收到。
4、為了防止我們的廣播接收不到,我們自己寫的廣播接收器的權(quán)限必須要大,以防萬(wàn)一,我設(shè)置了1000。
下面上代碼,里面的注釋也比較詳細(xì)..
<?xml version="." encoding="utf-"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fanlei.cutnotedemo" >
//接收短信
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- action:name = 的名稱是固定的 -->
<receiver android:name=".NoteReceiver">
<intent-filter android:priority="">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
寫一個(gè)類,繼承BroadcastReceiver
Android--獲取短信的內(nèi)容,截取短信
package com.example.fanlei.cutnotedemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 廣播接收器
*/
public class NoteReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//判斷廣播消息
if (action.equals(SMS_RECEIVED_ACTION)){
Bundle bundle = intent.getExtras();
//如果不為空
if (bundle != null){
//將pdus里面的內(nèi)容轉(zhuǎn)化成Object[]數(shù)組
Object pdusData[] = (Object[]) bundle.get("pdus");
//解析短信
SmsMessage[] msg = new SmsMessage[pdusData.length];
for (int i = ;i < msg.length;i++){
byte pdus[] = (byte[]) pdusData[i];
msg[i] = SmsMessage.createFromPdu(pdus);
}
StringBuffer content = new StringBuffer();//獲取短信內(nèi)容
StringBuffer phoneNumber = new StringBuffer();//獲取地址
StringBuffer receiveData = new StringBuffer();//獲取時(shí)間
//分析短信具體參數(shù)
for (SmsMessage temp : msg){
content.append(temp.getMessageBody());
phoneNumber.append(temp.getOriginatingAddress());
receiveData.append(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS")
.format(new Date(temp.getTimestampMillis())));
}
/**
* 這里還可以進(jìn)行好多操作,比如我們根據(jù)手機(jī)號(hào)進(jìn)行攔截(取消廣播繼續(xù)傳播)等等
*/
Toast.makeText(context,phoneNumber.toString()+content+receiveData, Toast.LENGTH_LONG).show();//短信內(nèi)容
}
}
}
}
ps:android獲取短信所有內(nèi)容
public class GetMessageInfo {
List<MessageInfo> list;
Context context;
MessageInfo messageInfo;
public GetMessageInfo(Context context) {
list = new ArrayList<MessageInfo>();
this.context = context;
}
// --------------------------------收到的短息信息----------------------------------
public List<MessageInfo> getSmsInfos() {
final String SMS_URI_INBOX = "content://sms/inbox";// 收信箱
try {
ContentResolver cr = context.getContentResolver();
String[] projection = new String[] { "_id", "address", "person","body", "date", "type" };
Uri uri = Uri.parse(SMS_URI_INBOX);
Cursor cursor = cr.query(uri, projection, null, null, "date desc");
while (cursor.moveToNext()) {
messageInfo = new MessageInfo();
// -----------------------信息----------------
int nameColumn = cursor.getColumnIndex("person");// 聯(lián)系人姓名列表序號(hào)
int phoneNumberColumn = cursor.getColumnIndex("address");// 手機(jī)號(hào)
int smsbodyColumn = cursor.getColumnIndex("body");// 短信內(nèi)容
int dateColumn = cursor.getColumnIndex("date");// 日期
int typeColumn = cursor.getColumnIndex("type");// 收發(fā)類型 1表示接受 2表示發(fā)送
String nameId = cursor.getString(nameColumn);
String phoneNumber = cursor.getString(phoneNumberColumn);
String smsbody = cursor.getString(smsbodyColumn);
Date d = new Date(Long.parseLong(cursor.getString(dateColumn)));
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd " + "\n" + "hh:mm:ss");
String date = dateFormat.format(d);
// --------------------------匹配聯(lián)系人名字--------------------------
Uri personUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,phoneNumber);
Cursor localCursor = cr.query(personUri, new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup.PHOTO_ID,PhoneLookup._ID }, null, null, null);
System.out.println(localCursor.getCount());
System.out.println("之前----"+localCursor);
if (localCursor.getCount()!=0) {
localCursor.moveToFirst();
System.out.println("之后----"+localCursor);
String name = localCursor.getString(localCursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
long photoid = localCursor.getLong(localCursor.getColumnIndex(PhoneLookup.PHOTO_ID));
long contactid = localCursor.getLong(localCursor.getColumnIndex(PhoneLookup._ID));
messageInfo.setName(name);
// 如果photoid 大于0 表示聯(lián)系人有頭像 ,如果沒(méi)有給此人設(shè)置頭像則給他一個(gè)默認(rèn)的
if (photoid > 0) {
Uri uri1 = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,contactid);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri1);
messageInfo.setContactPhoto(BitmapFactory.decodeStream(input));
} else {
messageInfo.setContactPhoto(BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_launcher));
}
}else{
messageInfo.setName(phoneNumber);
messageInfo.setContactPhoto(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher));
}
localCursor.close();
messageInfo.setSmsContent(smsbody);
messageInfo.setSmsDate(date);
list.add(messageInfo);
}
} catch (SQLiteException e) {
e.printStackTrace();
}
return list;
}
}
以上內(nèi)容是小編給大家分享的Android開發(fā)獲取短信的內(nèi)容并截取短信,希望大家喜歡。
- Android Bitmap的截取及狀態(tài)欄的隱藏和顯示功能
- Android實(shí)現(xiàn)bitmap指定區(qū)域滑動(dòng)截取功能
- android 手機(jī)截取長(zhǎng)屏實(shí)例代碼
- 解析Android截取手機(jī)屏幕兩種實(shí)現(xiàn)方案
- Android實(shí)現(xiàn)拍照截取和相冊(cè)圖片截取
- Android個(gè)人中心的頭像上傳,圖片編碼及截取實(shí)例
- Android 仿QQ頭像自定義截取功能
- Android中截取當(dāng)前屏幕圖片的實(shí)例代碼
- Android截取視頻幀并轉(zhuǎn)化為Bitmap示例
- Android截取指定View為圖片的實(shí)現(xiàn)方法
相關(guān)文章
Android開發(fā)一行代碼解決安卓重復(fù)點(diǎn)擊
這篇文章主要為大家介紹了Android開發(fā)一行代碼解決安卓重復(fù)點(diǎn)擊,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Android 快速實(shí)現(xiàn)防止網(wǎng)絡(luò)重復(fù)請(qǐng)求&按鈕重復(fù)點(diǎn)擊的方法
下面小編就為大家分享一篇Android 快速實(shí)現(xiàn)防止網(wǎng)絡(luò)重復(fù)請(qǐng)求&按鈕重復(fù)點(diǎn)擊的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android三種實(shí)現(xiàn)定時(shí)器的方法
本文給大家分享了3種Android實(shí)現(xiàn)定時(shí)器的方法的示例,,需要的朋友可以參考下2015-02-02
Android從網(wǎng)絡(luò)中獲得一張圖片并顯示在屏幕上的實(shí)例詳解
這篇文章主要介紹了Android從網(wǎng)絡(luò)中獲得一張圖片并顯示在屏幕上的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08
Android ListView ImageView實(shí)現(xiàn)單選按鈕實(shí)例
這篇文章主要介紹了Android ListView ImageView實(shí)現(xiàn)單選按鈕的相關(guān)資料,需要的朋友可以參考下2016-10-10
Android編程之簡(jiǎn)單計(jì)時(shí)器實(shí)現(xiàn)方法
這篇文章主要介紹了Android編程之簡(jiǎn)單計(jì)時(shí)器實(shí)現(xiàn)方法,涉及Android開發(fā)中ContextMenu及Chronometer的相關(guān)使用技巧,需要的朋友可以參考下2016-01-01
詳解如何使用VisualStudio高效開發(fā)調(diào)試AndroidNDK
這篇文章主要介紹了詳解如何使用VisualStudio高效開發(fā)調(diào)試AndroidNDK,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
Android Flutter實(shí)現(xiàn)視頻上滑翻頁(yè)效果的示例代碼
我們?cè)诙桃曨l應(yīng)用中經(jīng)常會(huì)看到不停上滑瀏覽下一條視頻的沉浸式交互效果,這種交互能夠讓用戶不停地翻頁(yè),直到找到喜歡的視頻內(nèi)容。本文將通過(guò)Flutter中的PageView組件實(shí)現(xiàn),感興趣的可以了解一下2022-10-10

