Android text message and phone Interception

Source: Internet
Author: User
SMS interception implementation

 
When the status of the mobile phone changes, various broadcasts occur. Such as battery information, text messages, and phone calls. To intercept text messages, you only need to receive system text message broadcasts. SMS broadcast is an ordered broadcast that can be delivered to the next broad receiver for processing. To shield the system from receiving text messages, you only need to interrupt Broadcast Transmission.

SMS broadcast android. provider. Telephony. SMS_RECEIVED

1. Define a broadcast Receiver
Public class SmsReceiver extends BroadcastReceiver
{

@ Override
Public void onReceive (Context context, Intent intent)
{
// If the message is received
If (intent. getAction (). equals ("android. provider. Telephony. SMS_RECEIVED "))
{
// Cancel broadcast (this line of code will make the system fail to receive the text message)
AbortBroadcast ();
StringBuilder sb = new StringBuilder ();
// Receives data transmitted by SMS.
Bundle bundle = intent. getExtras ();
// Determine whether data exists
If (bundle! = Null)
{
// All received SMS messages can be obtained through pdus.
Object [] pdus = (Object []) bundle. get ("pdus ");
// Construct the SMS object array and create the array size based on the received object Length
Smsmessage [] messages = new smsmessage [PDUS. Length];
For (INT I = 0; I <PDUS. length; I ++)
{
Messages [I] = smsmessage
. Createfrompdu (byte []) PDUS [I]);
}
// Combine the sent text message with custom information in stringbuilder
For (smsmessage message: messages)
{
Sb. append ("SMS Source :");
// Obtain the phone number for receiving the text message
Sb. append (message. getDisplayOriginatingAddress ());
Sb. append ("\ n ------ text message content ------ \ n ");
// Obtain the text message content
Sb. append (message. getDisplayMessageBody ());
}
}
Toast. makeText (context, sb. toString ()
, Toast. LENGTH_LONG). show ();
}

}

}

2. Configure Broadcast
<Cycler
Android: name = ". SmsReceiver">
<! -- Android: priority broadcast receiving priority -->
<Intent-filter android: priority = "800">
<Action android: name = "android. provider. Telephony. SMS_RECEIVED"/>
</Intent-filter>
</Cycler>
Add SMS receiving permission
<Uses-permission android: name = "android. permission. RECEIVE_SMS"/>

Phone Interception
1. Get mobile phone management objects
Final TelephonyManager telManager = (TelephonyManager) getSystemService (Context. TELEPHONY_SERVICE );

2. extends PhoneStateListener listens to the mobile phone status
Class MyPhoneStateListener extends PhoneStateListener
{
@ Override
Public void onCallStateChanged (int state, String incomingNumber)
{
Super. onCallStateChanged (state, incomingNumber );

Switch (state)
{
Case TelephonyManager. CALL_STATE_IDLE:
Am. setRingerMode (AudioManager. RINGER_MODE_NORMAL );
Log. I ("CALL_STATE_IDLE", "No activity" + "+ incomingNumber );
Break;
Case TelephonyManager. CALL_STATE_OFFHOOK:
Toast. makeText (getApplicationContext (), "Incoming call answered", Toast. LENGTH_SHORT). show ();
Log. I ("CALL_STATE_OFFHOOK", "disconnect. "+" "+ IncomingNumber );
Break;
Case TelephonyManager. CALL_STATE_RINGING:
 
// Set mute when the mobile phone receives incoming calls
Am. setRingerMode (AudioManager. RINGER_MODE_SILENT );


// Legitimate mode mAudioManager. setRingerMode (AudioManager. RINGER_MODE_NORMAL );
Toast. makeText (getApplicationContext (), "Incoming call blocked", Toast. LENGTH_SHORT). show ();
Log. I ("CALL_STATE_RINGING", "zhenling" + "+ incomingNumber );
 
Break;
}

}
}

3. register the listener

TelManager. listen (new MyPhoneStateListener (), PhoneStateListener. LISTEN_CALL_STATE );

// Add the permission to read the mobile phone status
<Uses-permission android: name = "android. permission. READ_PHONE_STATE"/>

Test class:

Public class TestSmsActivity extends Activity
{
Button but = null;
AudioManager am = null;
 
@ Override
Protected void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

 
Final TelephonyManager telManager = (TelephonyManager) getSystemService (Context. TELEPHONY_SERVICE );

Am = (AudioManager) getSystemService (Context. AUDIO_SERVICE );

But = (Button) findViewById (R. id. call );
But. setOnClickListener (new View. OnClickListener ()
{

@ Override
Public void onClick (View v)
{
Telmanager. Listen (New myphonestatelistener (), phonestatelistener. listen_call_state );
 
Toast. maketext (getapplicationcontext (), "disabled", Toast. length_short). Show ();
}
});
}

Class myphonestatelistener extends phonestatelistener
{
@ Override
Public void oncallstatechanged (INT state, string incomingnumber)
{
Super. oncallstatechanged (State, incomingnumber );

Switch (state)
{
Case telephonymanager. call_state_idle:
Am. setringermode (audiomanager. ringer_mode_normal );
Log. I ("call_state_idle", "No activity" + "+ incomingnumber );
Break;
Case telephonymanager. call_state_offhook:
Toast. maketext (getapplicationcontext (), "Incoming call answered", Toast. length_short). Show ();
Log. I ("call_state_offhook", "disconnect. "+" "+ Incomingnumber );
Break;
Case telephonymanager. call_state_ringing:
 
// Set mute
Am. setringermode (audiomanager. ringer_mode_silent );


// Legitimate mode maudiomanager. setringermode (audiomanager. ringer_mode_normal );
Toast. maketext (getapplicationcontext (), "Incoming call blocked", Toast. length_short). Show ();
Log. I ("call_state_ringing", "zhenling" + "+ incomingnumber );
 
Break;
}

}
}

 

 
}

 

SMS interception implementation

 
When the status of the mobile phone changes, various broadcasts occur. Such as battery information, text messages, and phone calls. To intercept text messages, you only need to receive system text message broadcasts. SMS broadcast is an ordered broadcast that can be delivered to the next broad receiver for processing. To shield the system from receiving text messages, you only need to interrupt Broadcast Transmission.

SMS broadcast android. provider. Telephony. SMS_RECEIVED

1. Define a broadcast Receiver
Public class SmsReceiver extends BroadcastReceiver
{

@ Override
Public void onReceive (Context context, Intent intent)
{
// If the message is received
If (intent. getAction (). equals ("android. provider. Telephony. SMS_RECEIVED "))
{
// Cancel broadcast (this line of code will make the system fail to receive the text message)
AbortBroadcast ();
StringBuilder sb = new StringBuilder ();
// Receives data transmitted by SMS.
Bundle bundle = intent. getExtras ();
// Determine whether data exists
If (bundle! = Null)
{
// All received SMS messages can be obtained through pdus.
Object [] pdus = (Object []) bundle. get ("pdus ");
// Construct the SMS object array and create the array size based on the received object Length
SmsMessage [] messages = new SmsMessage [pdus. length];
For (int I = 0; I <pdus. length; I ++)
{
Messages [I] = SmsMessage
. CreateFromPdu (byte []) pdus [I]);
}
// Combine the sent text message with custom information in StringBuilder
For (SmsMessage message: messages)
{
Sb. append ("SMS Source :");
// Obtain the phone number for receiving the text message
Sb. append (message. getDisplayOriginatingAddress ());
Sb. append ("\ n ------ text message content ------ \ n ");
// Obtain the text message content
Sb. append (message. getDisplayMessageBody ());
}
}
Toast. makeText (context, sb. toString ()
, Toast. LENGTH_LONG). show ();
}

}

}

2. Configure Broadcast
<Cycler
Android: name = ". SmsReceiver">
<! -- Android: priority broadcast receiving priority -->
<Intent-filter android: priority = "800">
<Action android: name = "android. provider. Telephony. SMS_RECEIVED"/>
</Intent-filter>
</Cycler>
Add SMS receiving permission
<Uses-permission android: name = "android. permission. RECEIVE_SMS"/>

Phone Interception
1. Get mobile phone management objects
Final TelephonyManager telManager = (TelephonyManager) getSystemService (Context. TELEPHONY_SERVICE );

2. extends PhoneStateListener listens to the mobile phone status
Class MyPhoneStateListener extends PhoneStateListener
{
@ Override
Public void onCallStateChanged (int state, String incomingNumber)
{
Super. onCallStateChanged (state, incomingNumber );

Switch (state)
{
Case TelephonyManager. CALL_STATE_IDLE:
Am. setRingerMode (AudioManager. RINGER_MODE_NORMAL );
Log. I ("CALL_STATE_IDLE", "No activity" + "+ incomingNumber );
Break;
Case TelephonyManager. CALL_STATE_OFFHOOK:
Toast. makeText (getApplicationContext (), "Incoming call answered", Toast. LENGTH_SHORT). show ();
Log. I ("CALL_STATE_OFFHOOK", "disconnect. "+" "+ IncomingNumber );
Break;
Case TelephonyManager. CALL_STATE_RINGING:
 
// Set mute when the mobile phone receives incoming calls
Am. setRingerMode (AudioManager. RINGER_MODE_SILENT );


// Legitimate mode mAudioManager. setRingerMode (AudioManager. RINGER_MODE_NORMAL );
Toast. makeText (getApplicationContext (), "Incoming call blocked", Toast. LENGTH_SHORT). show ();
Log. I ("CALL_STATE_RINGING", "zhenling" + "+ incomingNumber );
 
Break;
}

}
}

3. register the listener

TelManager. listen (new MyPhoneStateListener (), PhoneStateListener. LISTEN_CALL_STATE );

// Add the permission to read the mobile phone status
<Uses-Permission Android: Name = "android. Permission. read_phone_state"/>

Test class:

Public class testsmsactivity extends Activity
{
Button but = NULL;
Audiomanager AM = NULL;
 
@ Override
Protected void oncreate (bundle savedinstancestate)
{
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

 
Final telephonymanager telmanager = (telephonymanager) getsystemservice (context. telephony_service );

AM = (audiomanager) getsystemservice (context. audio_service );

But = (button) findviewbyid (R. Id. Call );
But. setonclicklistener (New View. onclicklistener ()
{

@ Override
Public void onclick (view V)
{
Telmanager. Listen (New myphonestatelistener (), phonestatelistener. listen_call_state );
 
Toast. maketext (getapplicationcontext (), "disabled", Toast. length_short). Show ();
}
});
}

Class myphonestatelistener extends phonestatelistener
{
@ Override
Public void oncallstatechanged (INT state, string incomingnumber)
{
Super. oncallstatechanged (State, incomingnumber );

Switch (state)
{
Case telephonymanager. call_state_idle:
Am. setringermode (audiomanager. ringer_mode_normal );
Log. I ("call_state_idle", "No activity" + "+ incomingnumber );
Break;
Case telephonymanager. call_state_offhook:
Toast. maketext (getapplicationcontext (), "Incoming call answered", Toast. length_short). Show ();
Log. I ("call_state_offhook", "disconnect. "+" "+ Incomingnumber );
Break;
Case telephonymanager. call_state_ringing:
 
// Set mute
Am. setringermode (audiomanager. ringer_mode_silent );


// Legitimate mode maudiomanager. setringermode (audiomanager. ringer_mode_normal );
Toast. maketext (getapplicationcontext (), "Incoming call blocked", Toast. length_short). Show ();
Log. I ("call_state_ringing", "zhenling" + "+ incomingnumber );
 
Break;
}

}
}

 

 
}

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.