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

来源:互联网 发布:下载马赛克软件 编辑:程序博客网 时间:2024/05/01 15:39

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

public class Main extends Activity{public class MyPhoneCallListener extends PhoneStateListener{@Overridepublic 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);}}@Overridepublic 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{@Overridepublic 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);}}@Overridepublic 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> 

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