Wednesday, December 11, 2013

android proximity sensor

download:  http://www.mediafire.com/download/83nnqx9cnhw5nsv/AndroidProximitySensor_110920a.zip







another
-------------------------------------------

01import android.app.Activity;
02import android.hardware.Sensor;
03import android.hardware.SensorEvent;
04import android.hardware.SensorEventListener;
05import android.hardware.SensorManager;
06import android.os.Bundle;
07import android.widget.ImageView;
08 
09public class SensorActivity extends Activity implements SensorEventListener {
10 private SensorManager mSensorManager;
11 private Sensor mSensor;
12 ImageView iv;
13 
14 @Override
15 protected void onCreate(Bundle savedInstanceState) {
16  // TODO Auto-generated method stub
17  super.onCreate(savedInstanceState);
18  setContentView(R.layout.sensor_screen);
19  mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
20  mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
21  iv = (ImageView) findViewById(R.id.imageView1);
22 }
23 
24 protected void onResume() {
25  super.onResume();
26  mSensorManager.registerListener(this, mSensor,
27    SensorManager.SENSOR_DELAY_NORMAL);
28 }
29 
30 protected void onPause() {
31  super.onPause();
32  mSensorManager.unregisterListener(this);
33 }
34 
35 public void onAccuracyChanged(Sensor sensor, int accuracy) {
36 }
37 
38 public void onSensorChanged(SensorEvent event) {
39  if (event.values[0] == 0) {
40   iv.setImageResource(R.drawable.near);
41  } else {
42   iv.setImageResource(R.drawable.far);
43  }
44 }
45}

Sunday, December 1, 2013

get display density in android

DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        switch (metrics.densityDpi) {
        case DisplayMetrics.DENSITY_LOW:
            break;
        case DisplayMetrics.DENSITY_MEDIUM:
            break;
        case DisplayMetrics.DENSITY_HIGH:
            PASSED_DISTANCE = 200;
            break;
        }

android multitouch or touch 2 button at a time by multitouch



download:   http://www.mediafire.com/download/3obheaff1ut7sgl/MultiTouchHandler.zip


import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

    Button button1, button2 ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = (Button)findViewById(R.id.button1);
        button2 = (Button)findViewById(R.id.button2);

        RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.rl1);

        button1.setOnTouchListener(new RelativeLayout.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent m) {
                handleTouch(m);
                return true;
            }
        });
       
        button2.setOnTouchListener(new RelativeLayout.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent m) {
                handleTouch(m);
                return true;
            }
        });
    }
   
    void handleTouch(MotionEvent m)
    {
           
           
            int pointerCount = m.getPointerCount();
           
            for (int i = 0; i < pointerCount; i++)
            {
                int x = (int) m.getX(i);
                int y = (int) m.getY(i);           
                int id = m.getPointerId(i);
                int action = m.getActionMasked();
                int actionIndex = m.getActionIndex();
                String actionString;
               
               
                switch (action)
                {
                    case MotionEvent.ACTION_DOWN:
                        actionString = "DOWN";
                        break;
                    case MotionEvent.ACTION_UP:
                        actionString = "UP";
                        break;   
                    case MotionEvent.ACTION_POINTER_DOWN:
                        actionString = "PNTR DOWN";
                        break;
                    case MotionEvent.ACTION_POINTER_UP:
                        actionString = "PNTR UP";
                        break;
                    case MotionEvent.ACTION_MOVE:
                        actionString = "MOVE";
                        break;
                    default:
                        actionString = "";
                }
               
                String touchStatus = "Action: " + actionString + " Index: " + actionIndex + " ID: " + id + " X: " + x + " Y: " + y+ " pointerCount="+pointerCount;
               
                if (pointerCount == 1)
                    button1.setText(touchStatus);
                else
                    button2.setText(touchStatus);
            }
    }

}

android phone status & subscriber id