Android开发获取短信的内容并截取短信

本文给大家介绍android开发获取短信内容并截取短息的相关内容,本文代码简单易懂,感兴趣的朋友一起学习吧

1、首先我们要写一个广播接收器,当我们的手机收到短信时,系统会自动发送一个广播,我们只需要接收到这条广播就可以了

2、在广播里面,我们重写的onReceive()方法,通过里面的Intent写到的Bundle就可以拿到短信的内容,

3、清单文件里面我们必须要添加权限,否则无法接收到。

4、为了防止我们的广播接收不到,我们自己写的广播接收器的权限必须要大,以防万一,我设置了1000。

下面上代码,里面的注释也比较详细..

   //接收短信      

写一个类,继承BroadcastReceiver

 Android--获取短信的内容,截取短信 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里面的内容转化成Object[]数组 Object pdusData[] = (Object[]) bundle.get("pdus"); //解析短信 SmsMessage[] msg = new SmsMessage[pdusData.length]; for (int i = ;i 

ps:android获取短信所有内容

 public class GetMessageInfo { List list; Context context; MessageInfo messageInfo; public GetMessageInfo(Context context) { list = new ArrayList(); this.context = context; } // --------------------------------收到的短息信息---------------------------------- public List 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");// 联系人姓名列表序号 int phoneNumberColumn = cursor.getColumnIndex("address");// 手机号 int smsbodyColumn = cursor.getColumnIndex("body");// 短信内容 int dateColumn = cursor.getColumnIndex("date");// 日期 int typeColumn = cursor.getColumnIndex("type");// 收发类型 1表示接受 2表示发送 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); // --------------------------匹配联系人名字-------------------------- 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 表示联系人有头像 ,如果没有给此人设置头像则给他一个默认的 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; } } 

以上内容是小编给大家分享的Android开发获取短信的内容并截取短信,希望大家喜欢。

以上就是Android开发获取短信的内容并截取短信的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 移动