Monday, 2 January 2012

Delete Message from Message Box in android

Description:


Below method will delete message/s from Message box between two dates, depends upon passed argument.


Required Permissions.: <uses-permission android:name="android.permission.READ_SMS"/>
                          <uses-permission android:name="android.permission.WRITE_SMS"/>


 Uri uri = Uri.parse("content://sms/inbox");//Delete only inbox message
//Uri uri = Uri.parse("content://sms/sent");//Delete only sent message
//Uri uri = Uri.parse("content://sms/draft");//Delete only draft message
String dateFormat="dd-MM-yyyy";//You can change date format as per your requirement.
    /**
     * Delete  sms from Message box between two dates
     * @param  Uri  Content uri for sent/inbox/draft sms
     *
     * @param from=  date from which day
     * @param to= To which day message
     */

    protected boolean deletMsgFromMessageBox(Uri uri,String fromDate, String toDate,Context mContext) {
        try {
            Uri uriSms = uri;
            Calendar calCurr = Calendar.getInstance();
            Calendar calFrom = Calendar.getInstance();
            Calendar calto = Calendar.getInstance();

            calFrom.setTime((Date) new SimpleDateFormat(dateFormat)
                    .parse(fromDate));
            calto.setTime((Date) new SimpleDateFormat(dateFormat).parse(toDate));

            

            Cursor c = mContext.getContentResolver().query(uriSms, nullnull,
                    nullnull);

            int co = 0;
            while (c.moveToNext()) {
                calCurr.setTime((Date) new SimpleDateFormat(dateFormat)
                        .parse(getDateFromCursor(c)));
                co++;

                if (calCurr.after(calFrom) && calCurr.before(calto)) {

                    int id = c.getInt(0);
                    int thread_id = c.getInt(1); // get the thread_id
                    mContext.getContentResolver().delete(
                            Uri.parse("content://sms/conversations/"
                                    + thread_id),
                            "thread_id=? and _id=?",
                            new String[] { String.valueOf(thread_id),
                                    String.valueOf(id) });
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

Fetch Contact Name by passing Contact Number

Description:

Below method will return Contact name from Contact Details.
If passed number is not saved with name, it will return 'Unknown'.

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


/**
     * Fetch Contact Name by passing Contact Number
     *
     * @param context
     * @param number
     *            = Contact Number whose Name you want from Contact Details
     * @return name = return name if it is there else return 'Unknown'
     */


    public static final String getContactNameFromNumber(Context context,
            String number) {

        String phNo = number; // phone number you already have
        String name = "Unknown";
        try {
            Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                    Uri.encode(phNo));
            Cursor cs = context.getContentResolver()
                    .query(uri, new String[] { PhoneLookup.DISPLAY_NAME },
                            nullnullnull);
            if (cs.getCount() > 0) {
                cs.moveToFirst();
                name = cs.getString(cs.getColumnIndex(PhoneLookup.DISPLAY_NAME));
                cs.close();
            } else {

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return name;
    }

Sunday, 1 January 2012

Android Image Rotation

Description:


Below method will rotate image to 45 degrees.

Note: Don't forget to create ImageView in your layout xml file with android:id="@+id/imageView1

     /**
     * Rotate image to 45 degrees.
     */

    public void RotateImage(){
        ImageView image;
        Bitmap bMap;
        Matrix matrix;
        
        //Get ImageView from layout xml file
        image = (ImageView) findViewById(R.id.imageView1);
        
        //Decode Image using Bitmap factory.
        bMap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        
        //Create object of new Matrix.
        matrix = new Matrix();
        
        //set image rotation value to 45 degrees in matrix.
        matrix.postRotate(45);

        //Create bitmap with new values.
        Bitmap bMapRotate = Bitmap.createBitmap(bMap, 00,
                bMap.getWidth(), bMap.getHeight(), matrix, true);
        
        //put rotated image in ImageView.
        image.setImageBitmap(bMapRotate);
    }
  

DTMF Tone Generator Example

Description:

Below method will play '1' DTMF tone for 2 seconds.

    /**
     * Play 1 DTMF tone for 2 seconds.
     */

    public void PlayDtfmTone(){
        
        ToneGenerator toneGenerator= new   ToneGenerator(AudioManager.STREAM_DTMF,ToneGenerator.MAX_VOLUME);
        //this will play tone for 2 seconds.
        toneGenerator.startTone(ToneGenerator.TONE_DTMF_12000);
    
   }