1. Listening and broadcasting
Disadvantage, because the reason for the priority may not be received.
Code:
public static final String TAG = "Imichatsmsreceiver";
public static final String sms_received_action = "Android.provider.Telephony.SMS_RECEIVED";
public void OnReceive (context context, Intent Intent) {if (Intent.getaction (). Equals (Sms_received_action)) {
smsmessage[] messages = getmessagesfromintent (intent); for (Smsmessage message:messages) {String text = message.getoriginatingaddress () + ":" + Mess Age.getdisplayoriginatingaddress () + ":" + message.getdisplaymessagebody () + ":" + message.gettimest
Ampmillis ();
String num = message.getoriginatingaddress ();
LOG.I (TAG, "-------------" + text);
Toast.maketext (context, text, Toast.length_long). Show ();
SendMessage (num, "from" + num + "SMS has received", context); Context.getcontentresolver (). Registercontentobserver (Uri.parse ("content://sms/"), True, new Smsobserver (new
Handler (), context)); }} public void SendMeSsage (String num, string text, context context) {Smsmanager Smsmanager = Smsmanager.getdefault ();
Pendingintent sentintent = pendingintent.getbroadcast (context, 0, New Intent (), 0);
String strcontent = text;
Smsmanager.sendtextmessage (num, null, strcontent, sentintent, NULL);
Telephonymanager tl = (telephonymanager) context.getsystemservice (Context.telephony_service);
int itype = Tl.getphonetype ();
LOG.I (TAG, "-------------" + "current card type is:" + itype);
if (Itype = = telephonymanager.phone_type_gsm) {toast.maketext (context, "current card type: GSM", Toast.length_long). Show (); else if (Itype = = TELEPHONYMANAGER.PHONE_TYPE_CDMA) {toast.maketext (context, "current card type: CDMA", Toast.)
Length_long). Show (); else if (Itype = = Telephonymanager.phone_type_none) {toast.maketext (context, "current card type: NONE", toast.length_
LONG). Show (); } public final smsmessage[] Getmessagesfromintent (Intent Intent) {object[] messages = (OBject[]) Intent.getserializableextra ("PDUs");
byte[][] Pduobjs = new byte[messages.length][];
for (int i = 0; i < messages.length i++) {pduobjs[i] = (byte[)) messages[i];
} byte[][] PDUs = new byte[pduobjs.length][];
int pducount = Pdus.length;
smsmessage[] msgs = new Smsmessage[pducount];
for (int i = 0; i < Pducount i++) {pdus[i] = pduobjs[i];
Msgs[i] = SMSMESSAGE.CREATEFROMPDU (Pdus[i]);
return msgs;
}
2, the use of observation methods, listening to the SMS database
public class Smsobserver extends Contentobserver {private context mcontext;
Public Smsobserver (Handler Handler) {super (Handler);
Mcontext = context;
} public void OnChange (Boolean selfchange) {Super.onchange (selfchange);
Cursor Cursor =null; try {//Read the message in the Inbox cursor = Mcontext.getcontentresolver (). Query (Uri.parse ("Content://sms/inbox"), NULL, NU
ll, NULL, "date desc");
String body;
Boolean Hasdone =false; if (cursor!=null) {while (Cursor.movetonext ()) {BODY = cursor.getstring (Cursor.getcolu
Mnindex ("body"));
if (body!=null)//&& body.equals ("" Startmyactivity "" {Hasdone =true;
Break
} if (Hasdone) {break;
catch (Exception e) {e.printstacktrace ()}}}; finally {if (cursor!=null) cursor.close ();
}
}
}
Permissions to use:
<uses-permission android:name= "Android.permission.SEND_SMS"/>
<uses-permission android:name= " Android.permission.RECEIVE_SMS "/>
<uses-permission android:name=" Android.permission.READ_SMS "/>"