最近由于项目需要,添加来点反转静音功能,先将代码写出来,往大家给点意见
1.首先是一个主要的服务类,service
package com.create.wxb;
import java.util.List;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
public class TurnSilentService extends Service {
private TelephonyManager telephonyMgr;
private SensorManager sensorMgr;
private AudioManager audioMgr;
private SensorEventListener sensorListener;
private PhoneCallListener phoneListener;
private boolean ringing = false;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Sensor orientation = null;
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
telephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
sensorMgr = (SensorManager)this.getSystemService(SENSOR_SERVICE);
audioMgr = (AudioManager)this.getSystemService(AUDIO_SERVICE);
phoneListener = new PhoneCallListener();
telephonyMgr.listen(phoneListener, PhoneCallListener.LISTEN_CALL_STATE);
sensorListener = new SensorListener();
List<Sensor> sensorList = sensorMgr.getSensorList(Sensor.TYPE_ALL);
int size = sensorList.size();
for (int i = 0; i<size ;i++) {
int type = sensorList.get(i).getType();
if (type == Sensor.TYPE_ACCELEROMETER) {
orientation = sensorList.get(i);
break;
}
}
if (orientation != null) {
sensorMgr.registerListener(sensorListener,orientation, SensorManager.SENSOR_DELAY_NORMAL);
} else {
stopSelf();
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
telephonyMgr.listen(phoneListener, 0);
sensorMgr.unregisterListener(sensorListener);
}
private class PhoneCallListener extends PhoneStateListener{
@Override
public void onCallStateChanged(int state, String incomingNumber){
switch (state) {
case TelephonyManager.CALL_STATE_IDLE://电话处于待机状态
ringing = false;
break;
case TelephonyManager.CALL_STATE_OFFHOOK://电话通话中
ringing = false;
break;
case TelephonyManager.CALL_STATE_RINGING://有电话来电
ringing = true;
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
class SensorListener implements SensorEventListener{
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.values[2] < 0 && ringing) {//电话正在响铃中,且手机面朝下
int ringerMode = AudioManager.RINGER_MODE_SILENT;
audioMgr.setRingerMode(ringerMode);
}else{
audioMgr.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
}
}
}
上面这个service主要负责监听手机来点,并实现反转静音功能,现在主要需要一个Activity 来启动这个service。
2.activity来启动service
package com.create.wxb;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.startService(new Intent("com.android.create.turnslicentservice"));
}
}
3.需要在androidManifest.xml 文件中声明activity和service
其中里面是这样写的
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.create.wxb"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".HelloWorldActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".TurnSilentService">
<intent-filter >
<action android:name="com.android.create.turnslicentservice" />
</intent-filter>
</service>
<receiver android:name=".TurnSilentBroadcastReceiver">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
</manifest>
其中大家看到了,里面有对电话状态和系统启动的使用权限
4.后面的那个TurnSilentBroadcastReceiver,这个主要是对系统气的监听,目的是让系统启动后是否对这个service进行启动。
package com.create.wxb;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class TurnSilentBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if (action.equals("android.intent.action.BOOT_COMPLETED")) {
}
}
}
以上是对上述功能的主要实现
。
总体要在settings里面内置的话,我们要考虑对是否开启反转静音的状态保存。具体的实现在这就不写了,
谢谢