Friday, June 7, 2013

Android Get Phone Contacts details with Contact Image | Code for getting phone contact detail in Android | How to get phone number in Android phone programmatic

Hello Friends!
Today I am going to share code for getting phone contact detail from your android phone.
It will pick all the phone number, email and profile picture also.
Just copy paste below code and enjoy..

1)MainActivity.java


package com.example.phonecontactdetail;

import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.widget.TextView;

public class MainActivity extends Activity {
 TextView textDetail;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  textDetail = (TextView) findViewById(R.id.textView1);
  readContacts();
 }

 public void readContacts() {
  StringBuffer sb = new StringBuffer();
  sb.append("......Contact Details.....");
  ContentResolver cr = getContentResolver();
  Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
    null, null, null);
  String phone = null;
  String emailContact = null;
  String emailType = null;
  String image_uri = "";
  Bitmap bitmap = null;
  if (cur.getCount() > 0) {
   while (cur.moveToNext()) {
    String id = cur.getString(cur
      .getColumnIndex(ContactsContract.Contacts._ID));
    String name = cur
      .getString(cur
        .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

    image_uri = cur
      .getString(cur
        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
    if (Integer
      .parseInt(cur.getString(cur
        .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
     System.out.println("name : " + name + ", ID : " + id);
     sb.append("\n Contact Name:" + name);
     Cursor pCur = cr.query(
       ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
       null,
       ContactsContract.CommonDataKinds.Phone.CONTACT_ID
         + " = ?", new String[] { id }, null);
     while (pCur.moveToNext()) {
      phone = pCur
        .getString(pCur
          .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
      sb.append("\n Phone number:" + phone);
      System.out.println("phone" + phone);
     }
     pCur.close();

     Cursor emailCur = cr.query(
       ContactsContract.CommonDataKinds.Email.CONTENT_URI,
       null,
       ContactsContract.CommonDataKinds.Email.CONTACT_ID
         + " = ?", new String[] { id }, null);
     while (emailCur.moveToNext()) {
      emailContact = emailCur
        .getString(emailCur
          .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
      emailType = emailCur
        .getString(emailCur
          .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
      sb.append("\nEmail:" + emailContact + "Email type:" + emailType);
      System.out.println("Email " + emailContact
        + " Email Type : " + emailType);

     }

     emailCur.close();
    }
    
    if (image_uri != null) {
     System.out.println(Uri.parse(image_uri));
     try {
      bitmap = MediaStore.Images.Media
        .getBitmap(this.getContentResolver(),
          Uri.parse(image_uri));
      sb.append("\n Image in Bitmap:" + bitmap);
      System.out.println(bitmap);

     } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }

    }
   
    
    sb.append("\n........................................");
   }

   textDetail.setText(sb);
  }
 }

}

2)activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="10dp"
        android:text="TextView" />
  
</RelativeLayout>

Please comment for suggestion and help!
Thanks!

Wednesday, June 5, 2013

Android How to get device current speed using GPS | Get current speed using Network and GPS in Android

This simple code for getting device current speed using GPS. you can use Network Provider also..



package com.geofence.alarm;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

import com.example.geofenceapp.R;

public class SpeedAlarmActivity extends Activity {
	Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_speed_alarm);
		// Acquire a reference to the system Location Manager
		LocationManager locationManager = (LocationManager) this
				.getSystemService(Context.LOCATION_SERVICE);

		// Define a listener that responds to location updates
		LocationListener locationListener = new LocationListener() {
			public void onLocationChanged(Location location) {
				location.getLatitude();
				Toast.makeText(context, "Current speed:" + location.getSpeed(),
						Toast.LENGTH_SHORT).show();

			}

			public void onStatusChanged(String provider, int status,
					Bundle extras) {
			}

			public void onProviderEnabled(String provider) {
			}

			public void onProviderDisabled(String provider) {
			}
		};
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
				0, locationListener);

	}

}

Thanks,