Android develops access to SMS content and intercepts SMS _android

Source: Internet
Author: User
Tags dateformat stringbuffer

1, first we have to write a broadcast receiver, when our mobile phone received a message, the system will automatically send a broadcast, we only need to receive this broadcast on the

2, in the broadcast, we rewrite the OnReceive () method, through the intent written in the bundle can get text message content,

3, the list file inside we have to add permissions, otherwise can not receive.

4, in order to prevent our broadcast received, we write the radio receiver must be large, just in case, I set 1000.

The following code, inside the comments are also more detailed.

 <?xml version= "." Encoding= "utf-"?> <manifest xmlns:android= "Http://schemas.android.com/apk/res/android" p Ackage= "Com.example.fanlei.cutnotedemo" >//Receive SMS <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" > <!--the name of Action:name = is fixed--> <receiver a Ndroid:name= ". Notereceiver "> <intent-filter android:priority=" "> <action android:name=" Android.provider.Tele Phony.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> 

to write a class that inherits Broadcastreceiver

android--get the content of SMS, intercept SMS 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;  /** * Broadcast receiver/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 ();
       Judge broadcast message if (Action.equals (sms_received_action)) {Bundle Bundle = Intent.getextras (); If it is not an empty if (bundle!= null) {//converts contents of PDUs into object[] array Object pdusdata[] = (object[)) Bundle.get (
         "PDUs");
         Parsing SMS 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 ()//Get SMS contents stringbuffer PhoneNumber = new StringBuffer ();//Get to
           Address StringBuffer receivedata = new StringBuffer ()//Get time//analyze SMS specific parameters 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 ())); /** * Here can also do a lot of operations, such as our mobile phone number to intercept (cancel broadcast continue to spread) and so on/Toast.maketext (context,phonenumber.to String () +content+receivedata, Toast.length_long). Show ()//SMS Content}}}

ps:android get all SMS content

public class Getmessageinfo {list<messageinfo> List; 
  Context context; 
  Messageinfo Messageinfo; 
    Public Getmessageinfo {list = new arraylist<messageinfo> (); 
  This.context = context; ///--------------------------------SMS received----------------------------------public list<messageinfo> GETSM Sinfos () {final String Sms_uri_inbox = "Content://sms/inbox";//Inbox try {contentresolver cr = Context.g 
      Etcontentresolver (); 
      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 (); 
        -----------------------information----------------int namecolumn = Cursor.getcolumnindex ("person");//Contact Name list number 
       int phonenumbercolumn = Cursor.getcolumnindex ("Address")/mobile number int smsbodycolumn = Cursor.getcolumnindex ("body");/sms content int datecolumn = Cursor.getcolumnindex ("date");//Date int typecolumn = Cursor.getcolumnindex ("type");/Send/Receive type 1 means accept 2 to send String nameId = cursor.getstring (Namec 
        Olumn); 
        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); --------------------------match the contact name--------------------------Uri Personuri = Uri.withappendedpath (contactscontr Act. 
        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 ("before----" +localcursor); 
          if (Localcursor.getcount ()!=0) {Localcursor.movetofirst (); 
          System.out.println ("after----" +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); If the photoid is greater than 0 to indicate that the contact has an avatar, if the avatar is not set for the person, give him a default if (PhotoID > 0) {Uri Uri1 = Contenturis.withapp 
            Endedid (Contactscontract.contacts.content_uri,contactid); 
            InputStream input = ContactsContract.Contacts.openContactPhotoInputStream (CR, URI1); 
          Messageinfo.setcontactphoto (Input) (bitmapfactory.decodestream); else {Messageinfo.setcontactphoto Bitmapfactory.decoderesource (context.getresources (), R.drawable.ic_launch 
    ER));      }}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;  } 
}

The above content is small to share the Android development to get SMS content and intercept SMS, I hope you like.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.