Search This Blog

Showing posts with label Advance Java. Show all posts
Showing posts with label Advance Java. Show all posts

Monday, 22 July 2013

Example with source code of Reading call History from android phone


This tutorial teach you to fetch all call from android phone (Outgoing call, Incoming call, Missed Call) with information like call duration, user, call type etc

Because this whole process is very simple so i just make one method to read all call log and save them inside a vector of HasMap.

Android provide CallLog.Calls.CONTENT_URI content provider to read and write about calls. Every attribute has a call name inside data base of call content provider. Read those column and iterate through the loop

This need some permission in android manifest

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

Function to read everything about calls

/** Result HasMap **/
    Vector<HashMap<String, Object>> mCallHostory = new Vector<HashMap<String, Object>>();
    private String name = "name", date = "date", duration = "duration",
            isRead = "is_read", type = "type";

    /** Function to read all calls **/

    public void getCallHistory() {
        Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI,
                null, null, null, null);
        cursor.moveToFirst();
        do {
            HashMap<String, Object> mTemp = new HashMap<String, Object>();
            /* Reading Name */
            String nameTemp = cursor.getString(cursor
                    .getColumnIndex(CallLog.Calls.CACHED_NAME));
            if (name.equalsIgnoreCase(""))
                mTemp.put(name, "Unknown");
            else
                mTemp.put(name, nameTemp);
            /* Reading Date */
            long dateTemp = cursor.getLong(cursor
                    .getColumnIndex(CallLog.Calls.DATE));
            mTemp.put(date, dateTemp);

            /* Reading duration */
            long durationTemp = cursor.getLong(cursor
                    .getColumnIndex(CallLog.Calls.DURATION));
            mTemp.put(duration, durationTemp);

            /* Reading already user see it or not */
            long IS_READ = cursor.getLong(cursor
                    .getColumnIndex(CallLog.Calls.IS_READ));
            mTemp.put(isRead, IS_READ);

            /* Reading Date */
            int typeTemp = cursor.getInt(cursor
                    .getColumnIndex(CallLog.Calls.TYPE));
            mTemp.put(type, typeTemp);

            /* Add one call Detail to Vector */
            mCallHostory.add(mTemp);
        } while (cursor.moveToNext());

        Log.e("Check", "Data");
    }

Reference Link

Android Developer

Wednesday, 17 April 2013

Simple notification example in android with custom sound file

Creating a simple  notification in  android   consists of few steps. NotificationCompat.Builder allow you set notification title, body and icon. then you can go forward to make it custom. See the below function. Call this where ever you want to use notification and set your custom values 

    private void createAndGenerateNotifcation() {
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Notification TItile").setContentText("Body");
        mBuilder.setAutoCancel(true);
        try {
            Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"
                    + R.raw.yourfile);
            mBuilder.setSound(uri);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // Intent resultIntent = new Intent(this, Notification.class);
        Intent resultIntent = new Intent();
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(Notification.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify((int) System.currentTimeMillis(),
                mBuilder.build());
    }

Structure of Notification in Android

  1. Content title
  2. Large icon
  3. Content text
  4. Content info
  5. Small icon
  6. Time   that the notification was issued. You can set an explicit value with setWhen(); if you don't it defaults to the time that the system received the notification.

Lets have a look on description 

Setting Notification custom sound - Keep your file inside raw folder of res and give URI to notification like this

        try {
            Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"
                    + R.raw.yourfile);
            mBuilder.setSound(uri);
        } catch (Exception e) {
            e.printStackTrace();
        }

You can set auto cancel to remove when user tap on this       

    mBuilder.setAutoCancel(true); 

Starting your own activity while tap on notification - set your activity
name inside intent

Intent resultIntent = new Intent(this, Youractivty.class); 

Reference URL

Monday, 22 October 2012

Advance Java : Inner Classes Concept in Java

Inner classes was introduces in JDK 1.1. And Inner class refer to a class appear with in

• class declarations
• method bodies
• expressions

Reason To Use Nested Classes From docs.oracle.com

Logical grouping of classes—If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.

Increased encapsulation—Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.

More readable, maintainable code—Nesting small classes within top-level classes places the code closer to where it is used.

Scope of an Inner class differ according to which type of Inner class you are using. We have four type in Inner Class

  •   Member Inner (MI) Classes
  •   Local Inner (LI) Classes
  •   Anonymous Inner (AI) Classes
  •   Static Inner (SI) Classes


  Let see one Example of Member Inner Class

public class A {

 public static void main(String[] args) {
  /**
   * Accessing Member Inner Classes
   */
  MemberInnerClass member = new A().new MemberInnerClass();
  member.printMessage();
 }

 public class MemberInnerClass {

  public void printMessage() {
   System.out.print("I am inside Member Inner Claas");
  }
 }
} 
  
 To accessing MI you have to create Object of Parent class(outer class). You can make MI Read Only by creating on private constructor

Local Inner Class is created inside a Method and its scope remains same method Member Variables


public class A {

 public static void main(String[] args) {
  /**
   * Accessing Local Inner Classes
   */
  new A().LocalLimit("I am inside Local Inner Class");
 }

 public void LocalLimit(final String message) {
  
  class LocalInnerCalss {
   public void printMessage() {
    System.out.print(message);
   }
  }
  new LocalInnerCalss().printMessage();
 }
}

Static Inner Class-- Its useful when modeling tightly coupled entities.Static class also provide Providing meaningful namespaces


public class StaticInner {
 public static void main(String[] args) {
  /**
   * Accessing Static Inner Classes
   */
  StaticInner.MemberInnerClass member = new StaticInner.MemberInnerClass();
  member.printMessage();
 }

 public static class MemberInnerClass {

  public void printMessage() {
   System.out.print("I am inside Static Inner Claas");
  }
 }
}

About Anonymous Inner Class, I will explain in a separate article as its need more explanation.

Now At the End some Use full tips to remember

Remember while declaring an inner class
• MI and SI appear within the body of a class
• LI and AI appear within the body of a method

Remember while accessing outer fields
• Both MI, LI and AI can refer to fields of their enclosing scope
• A SI can’t, since it doesn’t have an enclosing instance!

Article You May Like

Synchronization Of thread in Java, Anonymous Inner Class Explanation

Android News and source code