Android 服務入門(電話監聽)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:onClick="Stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop" />

     <Button
        android:onClick="Start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start" />
</LinearLayout>


package com.example.listencall;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity
{

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Intent intent = new Intent(this, PhoneStatusService.class);
		startService(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void Stop(View v)
	{
		// 服務生命週期
		// creat startCommand start destory

		// 關閉生命週期
		Intent intent = new Intent(this, PhoneStatusService.class);
		stopService(intent);
	}
	
	public void Start(View v)
	{
		// 服務生命週期
		// creat startCommand start destory
		//屢次開啓若是沒有Destory則執行startCommand start
		// 關閉生命週期
		Intent intent = new Intent(this, PhoneStatusService.class);
		startService(intent);
	}
}



package com.example.listencall;

import java.io.IOException;

import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class PhoneStatusService extends Service
{
	// 長期後臺運行 若是用戶不終止 則持續運行

	@Override
	public void onCreate()
	{
		super.onCreate();
		Log.i("Service", "服務開啓了……");

		// 監視電話狀態
		// 電話管理器/服務
		TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
		// 監聽手機通話狀態
		tm.listen(new MyPhoneStatusLister(), PhoneStateListener.LISTEN_CALL_STATE);
	}

	private class MyPhoneStatusLister extends PhoneStateListener
	{
		private MediaRecorder recorder;

		@Override
		public void onCallStateChanged(int state, String incomingNumber)
		{
			try
			{
				switch (state)
				{
				case TelephonyManager.CALL_STATE_IDLE:// 空閒狀態 無通話 無響鈴
					if (null != recorder)
					{
						recorder.stop();
						recorder.reset(); // You can reuse the object by going
						recorder.release(); // Now the object cannot be reused
						System.out.println("錄音結束……");
					}
					break;
				case TelephonyManager.CALL_STATE_RINGING:// 響鈴狀態
					// 建立錄音機
					recorder = new MediaRecorder();
					System.out.println("來電號碼:" + incomingNumber + "錄音準備完畢……");
					recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
					recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
					recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
					recorder.setOutputFile("/sdcard/" + incomingNumber + ".aac");
					recorder.prepare();

					break;
				case TelephonyManager.CALL_STATE_OFFHOOK:// 通話狀態

					if (null != recorder)
					{
						System.out.println("正在錄音……");
						recorder.start(); // Recording is now started
					}

					break;
				default:
					break;
				}
			} catch (IllegalStateException e)
			{
				e.printStackTrace();
			} catch (IOException e)
			{
				e.printStackTrace();
			}

			super.onCallStateChanged(state, incomingNumber);
		}
	}

	@Override
	public void onDestroy()
	{
		super.onDestroy();
		Log.i("Service", "服務銷燬了……");
	}

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

	@Override
	@Deprecated
	public void onStart(Intent intent, int startId)
	{
		// TODO Auto-generated method stub
		Log.i("Service", "服務Start……");
		super.onStart(intent, startId);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId)
	{
		// TODO Auto-generated method stub
		Log.i("Service", "服務onStartCommand……");
		return super.onStartCommand(intent, flags, startId);
	}
	
}
相關文章
相關標籤/搜索