Android settings模块中添加来电反转静音功能

最近由于项目需要,添加来点Android反转静音功能,先将代码写出来,往大家给点意见

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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/pszgw.html