簡訊攔截實現
手機狀態發生改變時,系統會發生各種廣播。如電池資訊、收到簡訊、電話中等燈。如果要攔截手機的簡訊,只需要接受系統簡訊廣播即可。簡訊廣播屬於有序廣播,可以傳遞到下一個廣接收者處理。如果需要屏蔽系統接收簡訊,只需要中斷廣播的傳輸。
簡訊廣播 android.provider.Telephony.SMS_RECEIVED
1. 定義廣播接收器
public class SmsReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// 如果是接收到簡訊
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
{
// 取消廣播(這行代碼將會讓系統收不到簡訊)
abortBroadcast();
StringBuilder sb = new StringBuilder();
// 接收由SMS傳過來的資料
Bundle bundle = intent.getExtras();
// 判斷是否有資料
if (bundle != null)
{
// 通過pdus可以獲得接收到的所有簡訊訊息
Object[] pdus = (Object[]) bundle.get("pdus");
// 構建簡訊對象array,並依據收到的對象長度來建立array的大小
SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++)
{
messages[i] = SmsMessage
.createFromPdu((byte[]) pdus[i]);
}
// 將送來的簡訊合并自訂資訊於StringBuilder當中
for (SmsMessage message : messages)
{
sb.append("簡訊來源:");
// 獲得接收簡訊的電話號碼
sb.append(message.getDisplayOriginatingAddress());
sb.append("\n------簡訊內容------\n");
// 獲得簡訊的內容
sb.append(message.getDisplayMessageBody());
}
}
Toast.makeText(context, sb.toString()
, Toast.LENGTH_LONG).show();
}
}
}
2.配置廣播
<receiver
android:name=".SmsReceiver" >
<!--android:priority 廣播接收優先順序別 -->
<intent-filter android:priority="800" >
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver >
添加接收簡訊許可權
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
電話攔截
1.擷取手機管理對象
final TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
2. extends PhoneStateListener 監聽手機狀態
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", "無活動"+" "+incomingNumber);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(getApplicationContext(), "來電已經接聽", Toast.LENGTH_SHORT).show();
Log.i("CALL_STATE_OFFHOOK", "摘機。"+" "+incomingNumber);
break;
case TelephonyManager.CALL_STATE_RINGING:
//當手機接收來電時,設定靜音
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
//正當模式 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
Toast.makeText(getApplicationContext(), "來電已經攔截", Toast.LENGTH_SHORT).show();
Log.i("CALL_STATE_RINGING", "響鈴"+" "+incomingNumber);
break;
}
}
}
3. 註冊監聽器
telManager.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
//添加讀取手機狀態許可權
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
測試類別:
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(), "攔截已開啟", 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", "無活動"+" "+incomingNumber);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(getApplicationContext(), "來電已經接聽", Toast.LENGTH_SHORT).show();
Log.i("CALL_STATE_OFFHOOK", "摘機。"+" "+incomingNumber);
break;
case TelephonyManager.CALL_STATE_RINGING:
//設定靜音
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
//正當模式 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
Toast.makeText(getApplicationContext(), "來電已經攔截", Toast.LENGTH_SHORT).show();
Log.i("CALL_STATE_RINGING", "響鈴"+" "+incomingNumber);
break;
}
}
}
}
簡訊攔截實現
手機狀態發生改變時,系統會發生各種廣播。如電池資訊、收到簡訊、電話中等燈。如果要攔截手機的簡訊,只需要接受系統簡訊廣播即可。簡訊廣播屬於有序廣播,可以傳遞到下一個廣接收者處理。如果需要屏蔽系統接收簡訊,只需要中斷廣播的傳輸。
簡訊廣播 android.provider.Telephony.SMS_RECEIVED
1. 定義廣播接收器
public class SmsReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// 如果是接收到簡訊
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
{
// 取消廣播(這行代碼將會讓系統收不到簡訊)
abortBroadcast();
StringBuilder sb = new StringBuilder();
// 接收由SMS傳過來的資料
Bundle bundle = intent.getExtras();
// 判斷是否有資料
if (bundle != null)
{
// 通過pdus可以獲得接收到的所有簡訊訊息
Object[] pdus = (Object[]) bundle.get("pdus");
// 構建簡訊對象array,並依據收到的對象長度來建立array的大小
SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++)
{
messages[i] = SmsMessage
.createFromPdu((byte[]) pdus[i]);
}
// 將送來的簡訊合并自訂資訊於StringBuilder當中
for (SmsMessage message : messages)
{
sb.append("簡訊來源:");
// 獲得接收簡訊的電話號碼
sb.append(message.getDisplayOriginatingAddress());
sb.append("\n------簡訊內容------\n");
// 獲得簡訊的內容
sb.append(message.getDisplayMessageBody());
}
}
Toast.makeText(context, sb.toString()
, Toast.LENGTH_LONG).show();
}
}
}
2.配置廣播
<receiver
android:name=".SmsReceiver" >
<!--android:priority 廣播接收優先順序別 -->
<intent-filter android:priority="800" >
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver >
添加接收簡訊許可權
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
電話攔截
1.擷取手機管理對象
final TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
2. extends PhoneStateListener 監聽手機狀態
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", "無活動"+" "+incomingNumber);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(getApplicationContext(), "來電已經接聽", Toast.LENGTH_SHORT).show();
Log.i("CALL_STATE_OFFHOOK", "摘機。"+" "+incomingNumber);
break;
case TelephonyManager.CALL_STATE_RINGING:
//當手機接收來電時,設定靜音
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
//正當模式 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
Toast.makeText(getApplicationContext(), "來電已經攔截", Toast.LENGTH_SHORT).show();
Log.i("CALL_STATE_RINGING", "響鈴"+" "+incomingNumber);
break;
}
}
}
3. 註冊監聽器
telManager.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
//添加讀取手機狀態許可權
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
測試類別:
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(), "攔截已開啟", 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", "無活動"+" "+incomingNumber);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(getApplicationContext(), "來電已經接聽", Toast.LENGTH_SHORT).show();
Log.i("CALL_STATE_OFFHOOK", "摘機。"+" "+incomingNumber);
break;
case TelephonyManager.CALL_STATE_RINGING:
//設定靜音
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
//正當模式 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
Toast.makeText(getApplicationContext(), "來電已經攔截", Toast.LENGTH_SHORT).show();
Log.i("CALL_STATE_RINGING", "響鈴"+" "+incomingNumber);
break;
}
}
}
}