Android intercepts and deletes the SMS.

Source: Internet
Author: User
Tags greenweb

Directly give the code:


[Html]
Package com. zte. android. greenweb. launcher. service;
 
Import org. json. JSONException;
Import org. json. JSONObject;
 
Import android. content. BroadcastReceiver;
Import android. content. Context;
Import android. content. Intent;
Import android. database. Cursor;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. telephony. SmsMessage;
 
Public class InterceptSmsReciever extends BroadcastReceiver
{
 
// Broadcast message type
Public static final String SMS_RECEIVED_ACTION =
"Android. provider. Telephony. SMS_RECEIVED ";
 
@ Override
Public void onReceive (Context context, Intent intent)
{
String msgContent = "";
String functiontype = "";
Bundle bundle = intent. getExtras ();
Object messages [] = (Object []) bundle. get ("pdus ");
SmsMessage smsMessage [] = new SmsMessage [messages. length];
For (int n = 0; n <messages. length; n ++)
{
SmsMessage [n] = SmsMessage. createFromPdu (byte []) messages [n]);
MsgContent = smsMessage [n]. getMessageBody ();
Try
{
JSONObject json = new JSONObject (msgContent );
Functiontype = json. getString ("functiontype ");
If (functiontype. inclusignorecase ("TimelyManage "))//
{
String opennet = json. getString ("actiontype ");
Intent in = new Intent ();
In. putExtra ("OpenNet", opennet );
Context. sendBroadcast (intent );
DeleteSMS (context, msgContent );
This. abortBroadcast ();
}
}
Catch (JSONException e)
{
E. printStackTrace ();
}
}
}
 
Public void deleteSMS (Context context, String smscontent)
{
Try
{
// Prepare the uri address of the system SMS inbox
Uri uri = Uri. parse ("content: // sms/inbox"); // inbox
// Query all text messages in the inbox
Cursor isRead =
Context. getContentResolver (). query (uri, null, "read =" + 0,
Null, null );
While (isRead. moveToNext ())
{
// String phone =
// IsRead. getString (isRead. getColumnIndex ("address"). trim (); // obtain the sender
String body =
IsRead. getString (isRead. getColumnIndex ("body"). trim (); // get information
If (body. equals (smscontent ))
{
Int id = isRead. getInt (isRead. getColumnIndex ("_ id "));
 
Context. getContentResolver (). delete (
Uri. parse ("content: // sms"), "_ id =" + id, null );
}
}
}
Catch (Exception e)
{
E. printStackTrace ();
}
}
}

Package com. zte. android. greenweb. launcher. service;

Import org. json. JSONException;
Import org. json. JSONObject;

Import android. content. BroadcastReceiver;
Import android. content. Context;
Import android. content. Intent;
Import android. database. Cursor;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. telephony. SmsMessage;

Public class InterceptSmsReciever extends BroadcastReceiver
{

// Broadcast message type
Public static final String SMS_RECEIVED_ACTION =
"Android. provider. Telephony. SMS_RECEIVED ";

@ Override
Public void onReceive (Context context, Intent intent)
{
String msgContent = "";
String functiontype = "";
Bundle bundle = intent. getExtras ();
Object messages [] = (Object []) bundle. get ("pdus ");
SmsMessage smsMessage [] = new SmsMessage [messages. length];
For (int n = 0; n <messages. length; n ++)
{
SmsMessage [n] = SmsMessage. createFromPdu (byte []) messages [n]);
MsgContent = smsMessage [n]. getMessageBody ();
Try
{
JSONObject json = new JSONObject (msgContent );
Functiontype = json. getString ("functiontype ");
If (functiontype. inclusignorecase ("TimelyManage "))//
{
String opennet = json. getString ("actiontype ");
Intent in = new Intent ();
In. putExtra ("OpenNet", opennet );
Context. sendBroadcast (intent );
DeleteSMS (context, msgContent );
This. abortBroadcast ();
}
}
Catch (JSONException e)
{
E. printStackTrace ();
}
}
}

Public void deleteSMS (Context context, String smscontent)
{
Try
{
// Prepare the uri address of the system SMS inbox
Uri uri = Uri. parse ("content: // sms/inbox"); // inbox
// Query all text messages in the inbox
Cursor isRead =
Context. getContentResolver (). query (uri, null, "read =" + 0,
Null, null );
While (isRead. moveToNext ())
{
// String phone =
// IsRead. getString (isRead. getColumnIndex ("address"). trim (); // obtain the sender
String body =
IsRead. getString (isRead. getColumnIndex ("body"). trim (); // get information
If (body. equals (smscontent ))
{
Int id = isRead. getInt (isRead. getColumnIndex ("_ id "));

Context. getContentResolver (). delete (
Uri. parse ("content: // sms"), "_ id =" + id, null );
}
}
}
Catch (Exception e)
{
E. printStackTrace ();
}
}
}

[Html]
<! -- Register the aggreger and set the priority -->
<Cycler
Android: name = "com. zte. android. greenweb. launcher. service. InterceptSmsReciever"
Android: exported = "true">
<Intent-filter android: priority = "1000">
<Action android: name = "android. provider. Telephony. SMS_RECEIVED"/>
<Category android: name = "android. intent. category. DEFAULT"/>
</Intent-filter>
</Cycler>

<! -- Register the aggreger and set the priority -->
<Cycler
Android: name = "com. zte. android. greenweb. launcher. service. InterceptSmsReciever"
Android: exported = "true">
<Intent-filter android: priority = "1000">
<Action android: name = "android. provider. Telephony. SMS_RECEIVED"/>
<Category android: name = "android. intent. category. DEFAULT"/>
</Intent-filter>
</Cycler>


Permission:


[Html]
<Uses-permission android: name = "android. permission. RECEIVE_SMS"/>
<Uses-permission android: name = "android. permission. READ_SMS"/>
<Uses-permission android: name = "android. permission. WRITE_SMS"/>

<Uses-permission android: name = "android. permission. RECEIVE_SMS"/>
<Uses-permission android: name = "android. permission. READ_SMS"/>
<Uses-permission android: name = "android. permission. WRITE_SMS"/>

 

 

 

 

 

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.