Note that there are some explanatory texts on larger screens.

plurals
  1. POsetRingerMode(AudioManager.RINGER_MODE_VIBRATE) is not working as expected
    primarykey
    data
    text
    <pre class="lang-java prettyprint-override"><code>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()&gt;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); } } } </code></pre> <p>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.</p> <p>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.</p> <p>Can anyone help me with the solution?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload