Pages

Subscribe:

Labels

Friday, 30 September 2011

Important links


 Android Samples http://www.bogotobogo.com/android.html


1.Html Color Codes

2. JavaMail api documentation here

3.Developing Web applications with Tomcat and Eclipse here

4.Android Animation Tutorial here

5.How to make a exe file of Java Program ? here

6.Acces to the MMS part file content here

7.How to Read MMS Data in Android? here

8.Android and self-signed ssl certificates here

9.Sending Email in Android using JavaMail API here

10. Android Sample code projects HERE IMP.

11. Other language Samples HERE

12. File Uploading and downloading in android. HERE 

Wednesday, 28 September 2011

Android BroadcastReceiver for SMS

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.telephony.SmsMessage;
import android.telephony.TelephonyManager;
import android.widget.Toast;


public class SMSReceiver extends BroadcastReceiver {

    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    private SharedPreferences myPrefs;
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "SMS Received", Toast.LENGTH_LONG).show();
        myPrefs = context.getSharedPreferences("myPrefs",Context.MODE_PRIVATE);
            if (intent.getAction().equals(SMS_RECEIVED)) {
                //Toast.makeText(context, "SMS Received", Toast.LENGTH_LONG).show();
                Bundle bundle = intent.getExtras();
                if (bundle != null) {
                    Object[] pdus = (Object[])bundle.get("pdus");
                    final SmsMessage[] messages = new SmsMessage[pdus.length];
                    for (int i = 0; i < pdus.length; i++) {
                        messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                    }
                    if (messages.length > -1) {
                        String number=messages[0].getOriginatingAddress();
                          if(PhoneNumberUtils.compare(number1, number)||(number.endsWith(number1))){
                                   Intent intent1=new Intent(context,AudioHandler.class);
                                   intent1.putExtra("filename", details.getFilename());
                                    intent1.putExtra("from",details.getFrom());
                                    intent1.putExtra("subject", details.getSubject());
                                    intent1.putExtra("content", details.getContent());
                                    intent1.putExtra("time", details.getTime());
                                    context.startService(intent1);
                                   
                               }
                        }
                    }
             }
    }




And also declare  the bellow permission in the android manifestfile

 
<uses-permission android:name="android.permission.READ_CONTACTS"/>
 <uses-permission android:name="android.permission.RECEIVE_SMS"/>
 
  

Query the Android Contacts

This code snippet queries all the contacts from the android device and sort those contacts

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ContactActivity extends Activity {
    ArrayList<HashMap<String,String>> contactData=new ArrayList<HashMap<String,String>>();
    ListView nameList;
    private DBAdapter db;
    String contact="";
   
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setTitle("Wait....");
        setContentView(R.layout.deleteaccountlist);
        db=new DBAdapter(this);
        db.open();
        nameList=(ListView)findViewById(R.id.listView2);
        android.telephony.TelephonyManager mng=(TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        new QueryContacts().execute();
        nameList.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
                HashMap map=contactData.get(position);
                db.insertSMSContact((String)map.get("name"),(String)map.get("number"));
                db.insertSpeechDetails((String)map.get("number"), 0);
                setResult(Activity.RESULT_OK);
                finish();
            }
        });
    }
   
    class QueryContacts extends AsyncTask<Void,Void,Void> {
        Dialog dialog;
         @Override
        protected void onPreExecute( ) {
            dialog=ProgressDialog.show(ContactActivity.this,"Please wait...", "Loading the contacts..",true, true);
            contactData.clear();
       }
       
         @Override
         protected void onPostExecute(Void result) {
            SimpleAdapter contactsAdapter=new SimpleAdapter(ContactActivity.this,contactData,R.layout.contactview,new String[]{"name","number"},new int[]{R.id.contactName,R.id.contactNumber});
            nameList.setAdapter(contactsAdapter);
            dialog.cancel();
            setTitle("Select Contact:");
         }
         protected void onProgressUpdate(Void... values) {
         }

         @SuppressWarnings("unchecked")
        @Override
         protected Void doInBackground(Void... arg0) {
             ContentResolver cr = getContentResolver();
             Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
             while (cursor.moveToNext()) {
                 try{
                 String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                 String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                 String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                 if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                     Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
                     while (phones.moveToNext()) {
                         String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
                         HashMap<String,String> map=new HashMap<String,String>();
                         map.put("name", name);
                         map.put("number", phoneNumber);
                         contactData.add(map);
                     }
                     phones.close();
                 }
             }catch(Exception e){}
             }
           
           
             /*Cursor cur = cr.query(People.CONTENT_URI,null, null, null, null);
             while (cur.moveToNext()) {
                 try{
                 String id = cur.getString(cur.getColumnIndex(People._ID));
                 String name = cur.getString(cur.getColumnIndex(People.DISPLAY_NAME));
                // if (Integer.parseInt(cur.getString(cur.getColumnIndex(People.PRIMARY_PHONE_ID))) > 0) {
                     Cursor pCur = cr.query(Contacts.Phones.CONTENT_URI,    null,Contacts.Phones.PERSON_ID +" = ?",
                             new String[]{id}, null);
                     while (pCur.moveToNext()) {
                         HashMap<String,String> map=new HashMap<String,String>();
                         String phoneNumber=pCur.getString(pCur.getColumnIndex(Contacts.Phones.NUMBER));
                         map.put("name", name);
                         map.put("number", phoneNumber);
                         contactData.add(map);
                         contact=contact+":"+name;
                         contact=contact+":"+phoneNumber;
                     }
                 //}
                 }catch(Exception e){e.printStackTrace();}
             }
           
              /*
             while (cursor.moveToNext()) {
               
                   String name=cursor.getString(cursor.getColumnIndex(People.DISPLAY_NAME));
                   String phoneNumber = cursor.getString(cursor.getColumnIndex( People.NUMBER));
                   HashMap<String,String> map=new HashMap<String,String>();
                   map.put("name", name);
                   map.put("number", phoneNumber);
                  
                   if(phoneNumber!=null)
                       contactData.add(map);
                }*/
                Collections.sort(contactData, new Comparator(){
                    @Override
                    public int compare(Object o1, Object o2) {
                        HashMap map1=(HashMap)o1;
                        HashMap map2=(HashMap)o2;
                        String s1=(String)map1.get("name");
                        String s2=(String)map2.get("name");
                        return s1.compareTo(s2);
                    }
                });
           
             /*
             Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
                while (cursor.moveToNext()) {
                   String contactId = cursor.getString(cursor.getColumnIndex(
                   ContactsContract.Contacts._ID));
                   String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                   String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                   if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                       Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
                       while (phones.moveToNext()) {
                           String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
                           HashMap<String,String> map=new HashMap<String,String>();
                           map.put("name", name);
                           map.put("number", phoneNumber);
                           contactData.add(map);
                       }
                       phones.close();
                   }
                }
                Collections.sort(contactData, new Comparator(){
                    @Override
                    public int compare(Object o1, Object o2) {
                        HashMap map1=(HashMap)o1;
                        HashMap map2=(HashMap)o2;
                        String s1=(String)map1.get("name");
                        String s2=(String)map2.get("name");
                        return s1.compareTo(s2);
                    }
                });*/
            return null;
         }
    }
}



Also declare the permission in the android manifest file

<uses-permission android:name="android.permission.READ_CONTACTS"/>