0

I am trying to make an app which calls a certain number just by hovering palm over android device. I have created a Background service for doing so. But when I start the service and wave over my device, the app crashes! So my question in that is it possible to make a CERTAIN phone call by making use of change in Sensor readings? Here's my code of Service class

public class ProxService extends Service implements SensorEventListener {            SensorManager manager;
    Sensor prox;

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        manager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        prox = manager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
        manager.registerListener(ProxService.this,prox,SensorManager.SENSOR_DELAY_NORMAL);      

        return START_STICKY;        
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();

        manager.unregisterListener(this);
        Toast.makeText(getApplicationContext(),"Accelerometer Service Stopped!",0).show();
    }

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

        //Neglect it!       
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        float reading;

        reading = event.values[0];
        //AM I GIVING RIGHT CONDITION HERE?
        while(reading == 0.0){
            Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:+XXXXXXXXXX"));    
            startActivity(intent);
            break;
        }
    }
}

I have checked all necessary Permissions required in manifest!

mata
  • 361
  • 3
  • 14

1 Answers1

0

First, according to SensorEvent documentation values[0] contains measured sensor distance in centimeters.
Instead of while loop you should add condition whether hand is close enough:

// if hand is closer than 5 cm
if(reading < 5) {
    // start activity
}

That while condition is same as if(reading == 0.0), because of break sentence (it never loops).
Also you should never compare float or double with ==. Read this why.

Second, please update you question with Exception log, so we can see what is causing it.

Community
  • 1
  • 1
mata
  • 361
  • 3
  • 14
  • This is the change I did in code...... float reading; reading = event.values[0]; try{ if(reading < prox.getMaximumRange()){ Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:98974")); startActivity(intent); }} catch(Exception e){ e.printStackTrace(); Log.e("Proximity","Here is the Error :P"); Toast.makeText(getApplicationContext(),"Calling not Started!",0).show(); } Still it isn't working. It is causing some exception.How to check that exception,as i am running this app in actual device? – user3858511 Aug 07 '14 at 14:32
  • If this doesn't catch the exception, it is thrown somewhere else in your code. When exception in not caught, stack trace should be printed in Logcat. Paste that stack trace to your question. – mata Aug 08 '14 at 07:27