Android 6.0 listening SMS (with git really good, easy and easy to enjoy)

Source: Internet
Author: User

Reprint please indicate the source: Wang 亟亟 's way of Daniel

Recently in the last tenure has been squeezed a busy flight, but today is the last day, to help deal with a bug on the merit perfection. And then recently in a mobile phone communication +xx monitoring of the demo, which is a part of the SMS listening, and then just 6.0 of the authorization of some factors, then the implementation of this part of a separate to do this demo, by the day to find a good third-party library to contribute to everyone

Let's take a look at the running effect

First, configure the SMS field to filter the input

Input Box control

‘com.github.florent37:materialtextfield:1.0.5‘

And then use a custom RV to display/process the input content

    ‘com.nikhilpanju.recyclerviewenhanced:recyclerviewenhanced:1.0.0‘

Then the user will jump to the listening page, when there is a text message, shown as follows

Look at the project structure again.

A list of libraries to use

dependencies {compile filetree (dir :  ' Libs ' , include: [ ' *.jar ' ]) testcompile  ' junit:junit:4.12 '  compile  com.android.support:appcompat-v7 : 23.4.0 '  compile  ' com.apkfuns.logutils:library:1.4.2 '  compile  ' com.jakewharton:butterknife:8.1.0 '  compile  COM.GITHUB.NAVASMDC: Materialdesign:[email protected] '  apt  ' Com.jakewharton: butterknife-compiler:8.1.0 '  compile  com.nikhilpanju.recyclerviewenhanced: recyclerviewenhanced:1.0.0 '  compile  Com.github.florent37:materialtextfield : 1.0.5 '  compile  ' com.lovedise:permissiongen:0.0.6 ' } 

OK, to read the code step by step, start with the configuration

 Public  class settingactivity extends baseactivity {    @BindView(r.id.input) EditText input;@BindView(r.id.add) buttonrectangle add;@BindView(r.id.jump) Buttonrectangle jump;@BindView(R.id.recyclerview) Recyclerview Recyclerview;PrivateMainadapter Madapter;PrivateRecyclertouchlistener Ontouchlistener;//Data collection    PrivateList<string> list;@Override    intGetLayout () {returnr.layout.activity_setting; }@Override    voidInit () {Butterknife.bind ( This); List =NewArraylist<> (); Madapter =NewMainadapter ( This, list);        Recyclerview.setadapter (Madapter); Recyclerview.setlayoutmanager (NewLinearlayoutmanager ( This)); Ontouchlistener =NewRecyclertouchlistener ( This, Recyclerview); }@Override    voidLogic () {//Register sliding Delete behaviorOntouchlistener.setswipeoptionviews (R.id.edit). Setswipeable (R.ID.ROWFG, R.ID.ROWBG,NewRecyclertouchlistener.onswipeoptionsclicklistener () {@Override                     Public void onswipeoptionclicked(intViewID,intPosition) {if(ViewID = = R.id.edit)                            {list.remove (position);                        Madapter.notifydatasetchanged ();        }                    }                });    Recyclerview.addonitemtouchlistener (Ontouchlistener); }@Override    voidOnresumeinit () {//Refresh Adapter        if(List.size () >0) {madapter.notifydatasetchanged (); }    }@Override    voidOnresumelogic () {}@Override    protected void OnPause() {Super. OnPause ();//Releasing useless resourcesRecyclerview.removeonitemtouchlistener (Ontouchlistener); }@OnClick({r.id.add, r.id.jump}) Public void OnClick(View view) {Switch(View.getid ()) { CaseR.id.add:string value = Input.gettext (). toString (). Trim ();if(!value.isempty ())                    {List.add (value);                    Madapter.notifydatasetchanged (); Input.settext (""); } Break; CaseR.id.jump:if(List.size () >0) {Intent Intent =NewIntent (settingactivity. This, Mainactivity.class); Intent.putstringarraylistextra ("DataList", (arraylist<string>) list);                StartActivity (Intent); }Else{Toast.maketext (Getapplicationcontext (),"Please add filter content", Toast.length_short). Show (); } Break; }    }}

Specific important steps have been written in the note, the main people can look at this RV library, support partial click, Global click and custom Swipe delete in the https://github.com/ddwhan0123/Useful-Open-Source-Android RV part with Storage

And look at the main class.

 Public  class mainactivity extends baseactivity {    Private Static FinalString ACTION ="Android.provider.Telephony.SMS_RECEIVED";PrivateSmsbroadcastreceiver Msmsbroadcastreceiver;PrivateList<string> intentlist;@BindView(r.id.text) TextView text;@Override    intGetLayout () {returnR.layout.activity_main; }@Override    voidInit () {Butterknife.bind ( This);//Accept pass-through filter setIntentlist = Getintent (). Getstringarraylistextra ("DataList"); }@Override    voidLogic () {//AuthorizationPermissiongen.with (mainactivity. This). Addrequestcode ( -). Permissions (Manifest.permission.RECEIVE_SMS, Manifest.per Mission. read_sms). request ();//Generate broadcast processingMsmsbroadcastreceiver =NewSmsbroadcastreceiver (); Msmsbroadcastreceiver.setonreceivedmessagelistener (NewSmsbroadcastreceiver.messagelistener () {@Override             Public void onreceived(Phonemessage phonemessage) { for(intK =0; K < Intentlist.size (); k++) {if(Phonemessage.getmsgcontent (). Contains (Intentlist.get (k))) {String msg = Phonemessage.getphonenumber () +"*"+ phonemessage.getmsgtime () +"*"+ phonemessage.getmsgcontent (); LOGUTILS.D ("---> Send object is"+ msg);                    Text.settext (msg);    }                }            }        }); }@Override    voidOnresumeinit () {}@Override    voidOnresumelogic () {}@Override    protected void OnDestroy() {Super. OnDestroy ();//Logout of the broadcast        if(Msmsbroadcastreceiver! =NULL) { This. Unregisterreceiver (Msmsbroadcastreceiver); Msmsbroadcastreceiver =NULL; }    }@PermissionSuccess(Requestcode = -) Public void Doregisterreceiver() {//Instantiate the filter and set the broadcast to be filteredIntentfilter Intentfilter =NewIntentfilter (ACTION); Intentfilter.setpriority ( +);//Registered broadcast         This. Registerreceiver (Msmsbroadcastreceiver, Intentfilter); }@PermissionFail(Requestcode = -) Public void Dofailregisterreceiver() {Toast.maketext ( This,"Contact permission are not granted", Toast.length_short). Show (); }@Override     Public void Onrequestpermissionsresult(intRequestcode, string[] permissions,int[] grantresults) {Permissiongen.onrequestpermissionsresult ( This, Requestcode, permissions, grantresults); }}

This class starts by requesting the authorization to read the text message, then turns on the broadcast or pops up the failed toast based on the callback of the authorization success (actual production, remember to always look at the broadcast in the absence, because the authorization behavior only in the first prompt to install)

This is a dynamic registration broadcast, after all perennial monitoring user behavior I was refused, life forced no way.

 Public  class smsbroadcastreceiver extends broadcastreceiver {    Private StaticMessageListener Mmessagelistener; Public Static FinalString sms_received_action ="Android.provider.Telephony.SMS_RECEIVED"; Public Smsbroadcastreceiver() {Super(); }@Override     Public void OnReceive(context context, Intent Intent) {LOGUTILS.D ("----> Smsbroadcastreceiver onreceive");if(Intent.getaction (). Equals (Sms_received_action)) {object[] PDUs = (object[]) Intent.getextras (). Get ("PDUs"); for(Object Pdu:pdus) {Smsmessage smsmessage = SMSMESSAGE.CREATEFROMPDU ((byte[]) PDU); String sender = Smsmessage.getdisplayoriginatingaddress ();//SMS contentString content = Smsmessage.getdisplaymessagebody ();LongDate = Smsmessage.gettimestampmillis (); Date tiemdate =NewDate (date); SimpleDateFormat SimpleDateFormat =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");                String time = Simpledateformat.format (tiemdate); Phonemessage Phonemessage =NewPhonemessage ();                Phonemessage.setphonenumber (sender);                Phonemessage.setmsgtime (time);                Phonemessage.setmsgcontent (content);                Mmessagelistener.onreceived (Phonemessage);            Abortbroadcast (); }        }    }//Callback interface     Public  interface messagelistener {        voidOnreceived (Phonemessage phonemessage); } Public void Setonreceivedmessagelistener(MessageListener MessageListener) { This. Mmessagelistener = MessageListener; }}

Broadcast and the main activity is through the interface to communicate with each other, and do not use a message or something, feel that the interface is relatively simple implementation, encapsulated a "message object", passing the mobile phone number/time/specific content

Filter behavior to the activity, do not want the broadcast internal too complex (can consider to change to MVP do)

Source Address: Https://github.com/ddwhan0123/BlogSample/tree/master/GetSMSDemo

Download Link: https://github.com/ddwhan0123/BlogSample/blob/master/GetSMSDemo/GetSMSDemo.zip?raw=true

By the right, you who collect my brother in Shanghai, you see accept me become not?! It's not a joke! (risking the risk of being seen by the leader)

Take my sweep and contact me below (indicate your intentions)

Android 6.0 listening SMS (with git really good, easy and easy to enjoy)

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.