0
public class ProximityService extends Service implements SensorEventListener{

    boolean hasproximity=false;
    SensorManager mSensorManager;
    AudioManager am;
    Sensor psensor;

    public ProximityService() {

    }
    @Override
    public void onCreate() {
        super.onCreate();
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
        if(mSensorManager.getSensorList(Sensor.TYPE_PROXIMITY).size()>0) {
            Toast.makeText(getApplicationContext(),"Proximity Sensor Detected",Toast.LENGTH_SHORT).show();
            hasproximity=true;
            psensor=mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
            Toast.makeText(getBaseContext(), "sensor event listener addded", Toast.LENGTH_SHORT).show();
            mSensorManager.registerListener(this,psensor,SensorManager.SENSOR_DELAY_UI);
        } else {
            hasproximity=false;
            Toast.makeText(getApplicationContext(),"No Proximity Sensor",Toast.LENGTH_SHORT).show();
            stopSelf();
        }
    }
    @Override

    public void onDestroy() {
        super.onDestroy();
        mSensorManager.unregisterListener(this);
        Toast.makeText(getBaseContext(), "service destroyed", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        float vals[]=event.values;
        float x; // light value is 0.0 then it is dark
        x=vals[0];
        Toast.makeText(getBaseContext(), "light:"+x, Toast.LENGTH_SHORT).show();
        if(x == 0.0) {
            Toast.makeText(getBaseContext(), "prox closed", Toast.LENGTH_SHORT).show();
            am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
        } else {
            Toast.makeText(getBaseContext(), "prox open", Toast.LENGTH_SHORT).show();
            am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        }
    }
}

This code is used for changing the phone ringing mode from ringing to vibrate if the proximity sensor is covered, i.e the light value received by the proximity sensor becomes zero.

The problem is if the phone is already ringing when we cover the proximity sensor, then the phone is going to silent mode, not vibrate mode.

Can anyone help me with the solution?

user990967
  • 444
  • 1
  • 6
  • 20

1 Answers1

0

You can use this code for change with >= Jerry Bean version:

Settings.System.putInt(getContentResolver(), "vibrate_when_ringing", 0); //vibrate off
Settings.System.putInt(getContentResolver(), "vibrate_when_ringing", 1); //vibrate on

Remember to add permission:

 <uses-permission android:name="android.permission.WRITE_SETTINGS" />
DzungPV
  • 1,561
  • 3
  • 18
  • 24