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);
	}
	
}





转载于:https://my.oschina.net/u/1246663/blog/199606

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值