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

Thursday, November 14, 2013

android json with post method response

try {
           
         URL url = new URL("http://www.htmlgoon.com/api/POST_JSON_Service.php");
           
            JSONObject jo = new JSONObject();

           
            jo.put("firstname", "abc");
            jo.put("lastname", "xyz");
            jo.put("city", "Bengalore");

           
//            jo.put("userID", "6268656283cfcc32b07340e5a9d7aceec747b316");
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url.toURI());
            // // Set up the header types needed to properly transfer JSON
            httpPost.setHeader("Content-Type", "application/json");
            httpPost.setHeader("Accept", "JSON");
            httpPost.setHeader("Accept-Encoding", "application/json");
            httpPost.setHeader("Accept-Language", "en-US");
            //
            // // Prepare JSON to send by setting the entity
            httpPost.setEntity(new StringEntity(jo.toString(), "UTF-8"));
            //

            // //
            // // Execute POST
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

            Log.d("===========", "HTTP: OK");
        } catch (Exception e) {
            Log.e("*********", "Error in http connection " + e.toString());
        }
        //
        // // Convert response to string
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "UTF-8"));

            sb = new StringBuilder();

            String line = null;
            //
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            is.close();

            result = sb.toString();
            //
            Log.e("&&&&&&&&&&&&", "result=" + result);
        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
                    .show();
        }
        // END Convert response to string

api testing webtie