随笔 - 23  文章 - 0 评论 - 14 trackbacks - 0

废话少说,直奔主题~!

结构如图:

mybroad.java代码:

public class mybroad extends BroadcastReceiver {

	static final String ACTION = "android.intent.action.BOOT_COMPLETED";

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		if (intent.getAction().equals(ACTION))

		{
			Intent service = new Intent(context, MyService.class);
		    context.startService(service);
			Log.d("d", "服务已经开始");
		}
	}
}

MyService.java代码:

public class MyService extends Service {
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		Log.d("d", "进入服务");
		TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
	    tm.listen(psl, PhoneStateListener.LISTEN_CALL_STATE);
		Log.d("d", "服务已启动");
		
	}
	PhoneStateListener psl = new PhoneStateListener() {
		private Boolean call_pd = false;
		private MediaRecorder mr;
		private String number;
		@Override
		public void onCallStateChanged(int state, String incomingNumber) {
			// TODO Auto-generated method stub
			// super.onCallStateChanged(state, incomingNumber);
			switch (state) {
			case TelephonyManager.CALL_STATE_IDLE:
				number = null;
				if ( mr != null && call_pd) {
					mr.stop();
					mr.reset();
					mr.release();
					call_pd = false;
				}
                Log.d("d", "录音结束");
				break;
			case TelephonyManager.CALL_STATE_OFFHOOK:
				try
				{
					mr = new MediaRecorder();
					mr.setAudioSource(MediaRecorder.AudioSource.MIC);
					mr.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
					mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
					SimpleDateFormat format = new SimpleDateFormat(
							"yyMMddHHmmss");//
					String fileName = this.number + "_"
							+ format.format(new Date());
					mr.setOutputFile("//sdcard//" + fileName + ".3gp");
					Log.d("d", Environment.getExternalStorageDirectory()
							.getAbsolutePath() + "/" + fileName + ".3gp");
					mr.prepare();
					mr.start();
					Log.d("d", "开始接电话");
					call_pd = true;
				}
				catch(Exception ex)
				{
					Log.d("d","有错误");
				}
				
				break;
			case TelephonyManager.CALL_STATE_RINGING:
				this.number = incomingNumber;
				Log.d("d", "电话进来");
				break;
			default:
				break;
			}
		}

	};

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

AndroidManifest.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="Tel_Listener.Jason"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="4" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
    <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
    
    <application android:icon="@drawable/icon" android:label="@string/app_name">
       
        <service android:name="MyService">
        </service>
        <receiver android:name="mybroad">
         <intent-filter>           
              <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <category android:name="android.intent.category.LAUNCHER"></category>
         </intent-filter>
        </receiver>
 
    </application>
</manifest>
posted on 2011-09-19 17:02 Jwc 阅读(...) 评论(...) 编辑 收藏