Android Monitor SMS (simultaneous monitoring of broadcasts and databases)

Source: Internet
Author: User
<span id="Label3"></p><p><p>The secret buckle, strongly condemns this behavior which violates the User's interest ...</p></p><p><p>The following is to introduce the principle of Android black button .... Android4.4 the following systems play games be careful, huh?</p></p><p><p>One way of the dark button: SMS ordering, that is, listening--------interception------------processing sms.</p></p><p><p>The second way: analog man-made operation (also known as analog traffic), through the background code simulation of human click behavior, secretly to the user to order business, by the carrier charge your fees, of course, which also need to involve monitoring/interception/processing of Sms. The principle of using this method is nothing more than HTTP processing web pages, but also related to access point switching problems, here is not explained in Detail.</p></p><p><p></p></p><p><p>Return to the Point: sometimes, our mobile phone program needs to listen to sms, when the condition A is met, not processing; when the condition B is met, it is set to read, and the text message is deleted when the condition C is met.</p></p><p><p>Note: Android 4.4 and later may be controlled by the user to control the permissions, if the user to close this program permissions, means that you can not listen to sms/operation SMS content</p></p><p><p>At present also like Xiaomi system in the installation, let the user to control permissions, 360来 monitoring priority intercept SMS and so on (about who first install who has priority, dynamic registration than static registration priority level, in these cases do not say) ...</p></p><p><p>The Android implementation of the listening SMS (while listening to broadcast and Database) code is as Follows:</p></p> <ol> <ol> <li><strong><strong> <strong>Intercept Broadcast<br></strong></strong></strong><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre>Package Com.javen.sms.receiver;import Java.text.simpledateformat;import Java.util.date;import java.util.TimeZone; Import Com.javen.util.interceptkeykeeper;import Android.content.broadcastreceiver;import android.content.Context; Import Android.content.intent;import android.os.bundle;import Android.telephony.smsmessage;import Android.util.Log ; Import Android.widget.toast;public class Smsreceiver extends Broadcastreceiver {private Context mcontext; public static final String sms_received_action = "android.provider.Telephony.SMS_RECEIVED"; public static final String sms_deliver_action = "android.provider.Telephony.SMS_DELIVER"; @Override public void OnReceive (context context, Intent Intent) {this.mcontext=context; Toast.maketext (context, "receive text messages executed ......", toast.length_long). show (); LOG.E ("smsreceiver, isorderedbroadcast () =", isorderedbroadcast () + ""); LOG.E ("smsreceiver onreceive ...", "receive SMS executed ..."); String action = intent.getaction (); If (sms_received_action.equals (ACTION) | | Sms_deliver_action.equals (ACTION) {toast.maketext (context, "start receiving SMS ...", toast.length_long). show (); LOG.E ("smsreceiver onreceive ...", "start receiving SMS ..."); Bundle bundle = Intent.getextras (); If (bundle! = Null) {object[] PDUs = (object[]) bundle.get ("pdus"); If (pdus! = null && pdus.length > 0) {smsmessage[] messages = new smsmessage[pdus.length] ; for (int i = 0; i < pdus.length; i++) {byte[] PDU = (byte[]) pdus[i]; messages[i] = SMSMESSAGE.CREATEFROMPDU (pdu); } for (smsmessage message:messages) {String content = Message.getmessageb Ody ();//get text message content String sender = Message.getoriginatingaddress ();//get The number of the outgoing message If (content.contains (intercepTkeykeeper.getinterceptkey (mcontext)) {toast.maketext (mcontext, "content:" +content, toast.length_ LONG). Show (); Setresultdata (null); This.abortbroadcast ();//abort}else if (sender.equals ("10010") | | sender.equals ("10086")) { Toast.maketext (mcontext, "content:" +content, toast.length_long). show (); This.abortbroadcast ();//abort} Date date = new Date (message.gettimestampmilli S ()); SimpleDateFormat format = new SimpleDateFormat ("yyyy-mm-dd HH:mm:ss"); Format.settimezone (timezone.gettimezone ("gmt+08:00")); String sendcontent = Format.format (date) + ":" + sender + "--" + content; LOG.E ("smsreceicer onreceive", sendcontent + ""); } } } } }}</pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p></p></p><strong><strong><br></strong></strong></li> <li><strong>open <strong> <strong>A service open listening database<br></strong></strong></strong><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre> Package Com.javen.service;import Android.app.service;import Android.content.contentresolver;import Android.content.intent;import Android.net.uri;import Android.os.ibinder;import Android.os.Process;import android.widget.toast;/** * @author Javen * Open a service open listening database */public class SMSService extends service {private smsobserver mobserver; @Override public ibinder onbind (Intent Intent) {return null; } @Override public void onCreate () {toast.maketext (this, "smsservice server started ......", toast.length_long). show (); Start Here contentresolver resolver = Getcontentresolver (); Mobserver = new Smsobserver (resolver, new Smshandler (this)); Resolver.registercontentobserver (uri.parse ("content://sms"), true,mobserver); } @Override public void OnDestroy () {super.ondestroy (); This.getcontentresolver (). Unregistercontentobserver (mobserver); Process.killprocess (process.mypid ()); }}</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p></p></p><strong><strong><br></strong></strong></li> <li><strong><strong> <strong> <strong>Database Watcher</strong></strong><br></strong></strong><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre>Package Com.javen.service;import Android.content.contentresolver;import Android.database.contentobserver;import Android.database.cursor;import android.net.uri;import android.os.message;import android.util.Log;/** * @author Javen * Database Watcher */public class Smsobserver extends Contentobserver {private contentresolver mresolver; Public Smshandler smshandler; Public smsobserver (contentresolver mresolver, smshandler handler) {super (handler); This.mresolver = mresolver; This.smshandler = handler; } @Override public void OnChange (boolean selfchange) {log.i ("smsobserver onChange", "smsobserver SMS has changed"); Cursor mcursor = mresolver.query (uri.parse ("content://sms/inbox"), new string[] {"_id", "address", "read", "body", "thread_id"}, "read=", new string[] {"0"}, "date desc"); if (mcursor = = Null) {return; } else {while (mcursor.movetonext ()) {smsinfo _smsinfo = new Smsinfo (); int _inindex = Mcursor.getcolumnindex ("_id"); If (_inindex! =-1) {_smsinfo._id = mcursor.getstring (_inindex); } int thread_idindex = Mcursor.getcolumnindex ("thread_id"); If (thread_idindex! =-1) {_smsinfo.thread_id = mcursor.getstring (thread_idindex); } int addressindex = Mcursor.getcolumnindex ("address"); If (addressindex! =-1) {_smsinfo.smsaddress = mcursor.getstring (addressindex); } int bodyindex = Mcursor.getcolumnindex ("body"); If (bodyindex! =-1) {_smsinfo.smsbody = mcursor.getstring (bodyindex); } int readindex = Mcursor.getcolumnindex ("read"); If (readindex! =-1) {_smsinfo.read = mcursor.getstring (readindex); } According to your interception strategy, determine whether the text messages are not manipulated, the text message is set to read, the text message Deleted//TODO System.out.println ("get the message content is:" +_smsinf O.tostring ()); LOG.I ("smsobserver ...", "get The text message content is:" +_smsinfo.tostring ()); Message msg = Smshandler.obtainmessage (); _smsinfo.action = 2;//0 does not operate sms, 1 text message is set to read, 2 will be deleted text message Msg.obj = _smsinfo; Smshandler.sendmessage (msg); }} if (mcursor! = Null) {mcursor.close (); Mcursor = null; } }}</pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p></p></p><strong><strong><br></strong></strong></li> <li><strong><strong>SMS Processing Class<br></strong></strong><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre> Package Com.javen.service;import Android.content.contentvalues;import Android.content.context;import Android.net.uri;import android.os.handler;import android.os.message;/** * @author javen * * SMS processing * */public CLA SS Smshandler extends Handler {private Context mcontext; Public Smshandler (context Context) {this.mcontext = context; } @Override public void Handlemessage (Message msg) {smsinfo smsinfo = (smsinfo) msg.obj; if (smsinfo.action = = 1) {contentvalues values = new Contentvalues (); Values.put ("read", "1"); Mcontext.getcontentresolver (). update (uri.parse ("content://sms/inbox"), values, "thread_id=?", New string[] {smsinfo.thread_id}); } else if (smsinfo.action = = 2) {Uri MUri = Uri.parse ("content://sms/"); Mcontext.getcontentresolver (). Delete (mUri, "_id=?", new string[] {smsinfo._id}); } }}</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p></p></p><strong><strong><br></strong></strong></li> <li><li><strong>Smsinfo data structure is mainly used for SMS interception<br></strong><span class="cnblogs_code_copy"></span><pre>Package com.javen.service;/** * Mainly used for SMS interception * * @author Javen * */public class Smsinfo {public String _id = ""; C7/>public String thread_id = ""; Public String smsaddress = ""; Public String smsbody = ""; Public String read = ""; public int action = 0;//1 is set to read, 2 means delete sms @Override public String toString () { return ' smsinfo [_id= ' + _id + ", thread_id=" + thread_id + ", smsaddress=" + smsaddress + ", smsbody=" + smsbody + ", read=" + read + ", AC tion= "+ Action +"] "; } }</pre><span class="cnblogs_code_copy"></span><p><strong><br></strong></p></li></li> <li><strong><strong>Required Permissions and configuration information<br></strong></strong><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><?xml version= "1.0" encoding= "utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" com.javen.sms "android:versioncode=" 1 "android:versionname=" 1.0 "> &LT;USES-SDK an droid:minsdkversion= "8" android:targetsdkversion= "/> <uses-permission android:name=" android.permis Sion. Send_sms "/> <uses-permission android:name=" Android.permission.RECEIVE_SMS "/> <!--<uses-per Mission android:name= "android.permission.BROADCAST_SMS"/> <uses-permission android:name= " Android.permission.READ_SMS "/> <uses-permission android:name=" Android.permission.WRITE_SMS "/>-< Application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/ap P_name "> <activity android:name=". Mainactivity "android:label=" @string/app_name "> <intent-filter> &Lt;action android:name= "android.intent.action.MAIN"/> <category android:name= "android.intent.categor Y.launcher "/> </intent-filter> </activity> <receiver Andro Id:name= "com.javen.sms.receiver.SmsReceiver" android:exported= "true" android:permission= "android.per Mission. Broadcast_sms "> <intent-filter android:priority=" 2147483647 "> <action android:name= "android.provider.Telephony.SMS_RECEIVED"/> <action android:name= "android.provider.Telephony.SMS_DEL IVER "/> <category android:name=" Android.intent.category.DEFAULT "/> </intent-filte r> </receiver> <service android:name= "com.javen.service.SmsService" ></service> </application></manifest></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p></p></p></li> <li><li><p><strong>the test starts in Mainactivity</strong> <strong>SMSService Send SMS to 10086 or 10010 to test</strong></p><p><strong></strong></p><pre> Intentservice = new Intent (this, smsservice.class); StartService (intentservice); Toast.maketext (this, "start service ...", 1). show (); </pre><p></p><p><br>The above is the Android 4.4 below the principle of the dark Button. Android 4.4 Above how to realize you can talk about communication in this blog .....</p></li></li> </ol> </ol><p><p>Android Monitor SMS (simultaneous monitoring of broadcasts and databases)</p></p></span>

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.