采用服务实现窃听电话

1.创建一个服务类,继承Service

public class CallStatusListener extends Service {

 @Override

 public IBinder onBind(Intent intent) {

  // TODO Auto-generated method stub

  return null;

 }

 @Override

 public void onCreate() {

  TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

  // 监听电话状态的变化

  manager.listen(new MyListener(), PhoneStateListener.LISTEN_CALL_STATE);

 }

 @Override

 public void onDestroy() {

  System.out.println(“服务销毁了。。。。。。。。。。。。。。”);

 }

 private class MyListener extends PhoneStateListener {

  private MediaRecorder recorder = null;

  @Override

  public void onCallStateChanged(int state, String incomingNumber) {

   switch (state) {

   // 空闲状态

   case TelephonyManager.CALL_STATE_IDLE:

    if (recorder != null) {

     System.out.println(“停止录音。。。。。。。。。。。。。”);

     recorder.stop();

     recorder.reset(); // You can reuse the object by going back

          // to

          // setAudioSource() step

     recorder.release(); // Now the object cannot be reused

     recorder = null;

    }

    break;

   // 响铃状态

   case TelephonyManager.CALL_STATE_RINGING:

    System.out.println(“准备录音。。。。。。。。。。。。。”);

    recorder = new MediaRecorder();

    recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);

    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);

    recorder.setOutputFile(“/sdcard/” + System.currentTimeMillis()

      + “.3gp”);

    try {

     recorder.prepare();

    } catch (Exception e) {

     System.out.println(“异常抛出。。。。。。。。。。。。。。。。。。”);

     e.printStackTrace();

    }

    break;

   // 通话状态

   case TelephonyManager.CALL_STATE_OFFHOOK:

    System.out.println(“recorder = ” + recorder);

    if (recorder != null) {

     System.out.println(“开始录音。。。。。。。。。。。。。”);

     recorder.start(); // Recording is now started

    }

    break;

   }

  }

 }

}

2.MainActivity启动服务

public class MainActivity extends Activity {

 @Override

 protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  Intent intent = new Intent(this, CallStatusListener.class);

  startService(intent);

 }

}

3.清单文件注册并授权

 <service android:name=”.CallStatusListener”></service>

<uses-permission android:name=” android.permission.READ_PHONE_STATE”/>

<uses-permission android:name=” android.permission.WRITE_EXTERNAL_STORAGE”/>

<uses-permission android:name=” android.permission.RECORD_AUDIO”/>

服务的生命周期

public class MainActivity extends Activity {

 @Override

 protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

 }

 public void start(View view) {

  Intent intent = new Intent(this, TestService.class);

  startService(intent);

 }

 public void stop(View view) {

  Intent intent = new Intent(this, TestService.class);

  stopService(intent);

 }

}

?

public class TestService extends Service {

 @Override

 public IBinder onBind(Intent intent) {

  // TODO Auto-generated method stub

  return null;

 }

 @Override

 public void onCreate() {

  Toast.makeText(getApplicationContext(), “onCreate()”, 0).show();

  super.onCreate();

 }

 @Override

 @Deprecated

 public void onStart(Intent intent, int startId) {

  // TODO Auto-generated method stub

  Toast.makeText(getApplicationContext(), “onStart()”, 0).show();

  super.onStart(intent, startId);

 }

 @Override

 public int onStartCommand(Intent intent, int flags, int startId) {

  // TODO Auto-generated method stub

  Toast.makeText(getApplicationContext(), “onStartCommandStart()”, 0)

    .show();

  return super.onStartCommand(intent, flags, startId);

 }

 @Override

 public void onDestroy() {

  Toast.makeText(getApplicationContext(), “onDestroy()”, 0).show();

  // TODO Auto-generated method stub

  super.onDestroy();

 }

}

android进程优先级&为什么使用服务

?绑定方式开启服务&调用服务的方法

1.Activity

public class MainActivity extends Activity {

 private MyConn myConn;

 private IService iService;

 @Override

 protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  this.myConn = new MyConn();

 }

 public void start(View view) {

  Intent intent = new Intent(this, SongService.class);

  startService(intent);

 }

 public void stop(View view) {

  Intent intent = new Intent(this, SongService.class);

  stopService(intent);

 }

 public void bind(View view) {

  Intent intent = new Intent(this, SongService.class);

  bindService(intent, this.myConn, Context.BIND_AUTO_CREATE);

 }

 public void unbind(View view) {

  unbindService(this.myConn);

 }

 public void change(View view) {

  iService.changSong(“一天一天等下去”);

 }

 private class MyConn implements ServiceConnection {

  @Override

  public void onServiceConnected(ComponentName name, IBinder service) {

   Toast.makeText(getApplicationContext(), “onServiceConnected”, 0)

     .show();

   iService = (IService) service;

  }

  @Override

  public void onServiceDisconnected(ComponentName name) {

   Toast.makeText(getApplicationContext(), “onServiceDisconnected”, 0)

     .show();

  }

 }

}

2.服务类

public class SongService extends Service {

 private MyIBinder myIBinder;

 @Override

 public IBinder onBind(Intent intent) {

  Toast.makeText(getApplicationContext(), “绑定服务”, 0).show();

  myIBinder = new MyIBinder();

  return this.myIBinder;

 }

 @Override

 public boolean onUnbind(Intent intent) {

  Toast.makeText(getApplicationContext(), “解除绑定服务”, 0).show();

  this.myIBinder = null;

  return false;

 }

 @Override

 public void onCreate() {

  Toast.makeText(getApplicationContext(), “启动服务”, 0).show();

 }

 @Override

 public void onDestroy() {

  Toast.makeText(getApplicationContext(), “停止服务”, 0).show();

 }

 public void changSong(String name) {

  Toast.makeText(getApplicationContext(), “开始播放:” + name, 0).show();

 }

 private class MyIBinder extends Binder implements IService{

  @Override

  public void changSong(String name) {

   SongService.this.changSong(name);

  }

 }

}

3.IService接口

public interface IService {

 public void changSong(String name);

}

服务生命周期(混合开启)

采用aidl绑定远程服务

一.提供服务的服务应用

1.定义一个服务类

public class AliPayService extends Service {

 @Override

 public IBinder onBind(Intent intent) {

  Toast.makeText(getApplicationContext(), “支付宝服务已绑定”, 0).show();

  return new MyIBinder();

 }

 @Override

 public boolean onUnbind(Intent intent) {

  Toast.makeText(getApplicationContext(), “支付宝服务已解绑”, 0).show();

  return super.onUnbind(intent);

 }

 @Override

 public void onCreate() {

  Toast.makeText(getApplicationContext(), “支付宝服务已创建”, 0).show();

  super.onCreate();

 }

 @Override

 public void onDestroy() {

  Toast.makeText(getApplicationContext(), “支付宝服务已销毁”, 0).show();

  super.onDestroy();

 }

 private String pay() {

  return “支付宝服务提供远程支付服务”;

 }

 private class MyIBinder extends IService.Stub {

  @Override

  public String callPay() throws RemoteException {

   String result = pay();

   return result;

  }

 }

}

2.在清单文件中配置

<service android:name=”.AliPayService” >

    <intent-filter>

        <action android:name=”com.itheima.alipay” />

    </intent-filter>

</service>

二.第三方应用

public class MainActivity extends Activity {

 private Intent intent;

 private IService iService;

 @Override

 protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  intent = new Intent();

  intent.setAction(“com.itheima.alipay”);

 }

 public void bind(View view) {

  this.bindService(intent, new MyConn(), Context.BIND_AUTO_CREATE);

 }

 public void call(View view) {

  try {

   String result = this.iService.callPay();

   Toast.makeText(getApplicationContext(), result, 0).show();

  } catch (RemoteException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

 }

 private class MyConn implements ServiceConnection {

  @Override

  public void onServiceConnected(ComponentName name, IBinder service) {

   iService = IService.Stub.asInterface(service);

  }

  @Override

  public void onServiceDisconnected(ComponentName name) {

   // TODO Auto-generated method stub

  }

 }

}

代码注册广播接受者&利用广播调用服务的方法

1.Activity 

public class MainActivity extends Activity {

 @Override

 protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  Intent intent = new Intent(this,MyService.class);

  startService(intent);

 }

 @Override

 protected void onDestroy() {

  Intent intent = new Intent(this,MyService.class);

  stopService(intent);

  super.onDestroy();

 }

 public void click(View view){

  Intent intent = new Intent();

  intent.setAction(“com.itheima.callmethod”);

  sendBroadcast(intent);

 }

}

2.Service

public class MyService extends Service{

 private Receiver receiver;

 @Override

 public IBinder onBind(Intent intent) {

  // TODO Auto-generated method stub

  return null;

 }

 @Override

 public void onCreate() {

  // 通过代码注册广播接收者

  IntentFilter filter = new IntentFilter();

  filter.addAction(“com.itheima.callmethod”);

  receiver = new Receiver();

  registerReceiver(receiver, filter );

  super.onCreate();

 }

 @Override

 public void onDestroy() {

  unregisterReceiver(receiver);

  receiver = null;

  super.onDestroy();

 }

 private void innerMethod() {

  Toast.makeText(this, “我是服务里边的内部方法”, 0).show();

 }

 private class Receiver extends BroadcastReceiver{

  @Override

  public void onReceive(Context context, Intent intent) {

   String action = intent.getAction();

   if(“com.itheima.callmethod”.equals(action)){

    innerMethod();

   }

   

  }

 }

}