其他分享
首页 > 其他分享> > 系统服务详解之电话服务、音频服务

系统服务详解之电话服务、音频服务

作者:互联网

接听电话时,会显示两个状态:来点状态和接听状态。下面示例演示了如何使用电话服务。

public class Main extends Activity
{
	public class MyPhoneCallListener extends PhoneStateListener
	{

		@Override
		public void onCallStateChanged(int state, String incomingNumber)
		{

			switch (state)
			{       //通话状态
				case TelephonyManager.CALL_STATE_OFFHOOK:
					Toast.makeText(Main.this, "正在通话...", Toast.LENGTH_SHORT)
							.show();
					break;
                                //来点状态
				case TelephonyManager.CALL_STATE_RINGING:
					Toast.makeText(Main.this, incomingNumber,
							Toast.LENGTH_SHORT).show();
					break;

			}
			super.onCallStateChanged(state, incomingNumber);
		}

	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		MyPhoneCallListener myPhoneCallListener = new MyPhoneCallListener();

		tm.listen(myPhoneCallListener, PhoneStateListener.LISTEN_CALL_STATE);


	}
}
配置文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="net.blogjava.mobile" android:versionCode="1"
	android:versionName="1.0">
	<application android:icon="@drawable/icon" android:label="@string/app_name">
		<activity android:name=".Main" android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>

	</application>

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

</manifest> 

我们了解了如何使用电话服务,现在可以配合音频服务来设计一个电话黑名单的功能。

public class Main extends Activity
{
	public class MyPhoneCallListener extends PhoneStateListener
	{

		@Override
		public void onCallStateChanged(int state, String incomingNumber)
		{
			AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
;
			switch (state)
			{
				case TelephonyManager.CALL_STATE_IDLE:
					audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
					break;
				case TelephonyManager.CALL_STATE_RINGING:
					if ("12345678".equals(incomingNumber))
					{
						audioManager
								.setRingerMode(AudioManager.RINGER_MODE_SILENT);
					}
					break;

			}
			super.onCallStateChanged(state, incomingNumber);
		}

	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		MyPhoneCallListener myPhoneCallListener = new MyPhoneCallListener();
		tm.listen(myPhoneCallListener, PhoneStateListener.LISTEN_CALL_STATE);

	}
}
配置文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="net.blogjava.mobile" android:versionCode="1"
	android:versionName="1.0">
	<application android:icon="@drawable/icon" android:label="@string/app_name">
		<activity android:name=".Main" android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>

	</application>

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

</manifest> 

这样当设定电话来电时,就会将手机设置为静音。



标签:服务,MyPhoneCallListener,音频,STATE,TelephonyManager,state,详解,CALL,public
来源: https://blog.51cto.com/u_9894631/2789303