Tuesday 19 November 2013

How to clear cache in Android Manually & Programmatically

What the android os does is when you exit an application it actually stores the apps cache on either your phones internal storage or on the sdcard; so what this does, is that when you load the application next time, it immediately loads the stored cache back into your active cache for the purpose of speeding up loading time. So in theory, stored cache is actually a good thing.
However, this is not always the case.
Its only really a beneficial feature for certain applications, like Google maps, where loading all those images normally takes a long time (relatively speaking of course.. its still really fast). An even better example would be your launcher app; your always loading the exact same homescreens everytime. But for other apps it actually is a bad thing and bogs your system down. Why? Because your taking over the fastest available memory on your device with stored data that might not be essential/relevant for what your doing the next time you load the application. A good example would be your web browser. The last time you ran it you were checking the news or watching youtube videos... but now your just doing a google search; you certainly don't need to load the cache from your last session. Get it? 

Clear Cache manually 
< Applications --> Manage Applications --> "My App" --> Clear Cache>> 
Clear cache programmatically

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

public class HelloWorld extends Activity {

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle *) {
      super.onCreate(*);
      setContentView(R.layout.main);
   }

   @Override
   protected void onStop(){
      super.onStop();
   }

   //Fires after the OnStop() state
   @Override
   protected void onDestroy() {
      super.onDestroy();
      try {
         trimCache(this);
      } catch (Exception e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }

   public static void trimCache(Context context) {
      try {
         File dir = context.getCacheDir();
         if (dir != null && dir.isDirectory()) {
            deleteDir(dir);
         }
      } catch (Exception e) {
         // TODO: handle exception
      }
   }

   public static boolean deleteDir(File dir) {
      if (dir != null && dir.isDirectory()) {
         String[] children = dir.list();
         for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
               return false;
            }
         }
      }

      // The directory is now empty so delete it
      return dir.delete();
   }

}
  
 

Wednesday 13 November 2013

How to save SMS to INBOX in Android Programmatically


There is below code

                ContentValues values = new ContentValues();
                values.put("address", "123456789");
                values.put("body", "foo bar");
                getContentResolver().insert(Uri.parse("content://sms/inbox"), values);

you can save SMS to Sent box also just require to few change.
Replace  ("content://sms/inbox") to ("content://sms/sent").



Below Android Permissions required

    <uses-permission android:name="android.permission.READ_SMS"/>
    <uses-permission android:name="android.permission.WRITE_SMS"/>
How to access Gmail Particular Label content programmatically


              javax.mail.Folder inbox = store.getFolder("SMS");
              inbox.open(Folder.READ_WRITE);
              Message messages[] = inbox.getMessages();
              int count =  messages.length;
              Log.d("SMS count ", ""+count);
              for (Message message : messages){
            // System.out.println(message);
              Log.d("inbox msssgggggg ", "Sub : "+message.getSubject()
                      +" Body :"+message.getContent().toString() +" From
                       :"+message.getFrom());
              }

          Where store is IMAPStore object