Thursday, May 31, 2012

Locale Change Receiver Android

Some times we need to know when the locale changing in our phone. We are localizing our app to be able to easily switch between languages. Everything works nicely except for some cached values that are actually in navigation. We have two options:

1. completely restart app on language change - in this case I need a notification and force restart

2. reload the activity - in this case it will require to replace string values and rebuild custom navigation state.

In both cases we need a way to find out when language switches.
In order to find out that we can use Broadcast receiver for this intent. We can use 'android.intent.action.LOCALE_CHANGED ' intent action for the same....



        
            
                
            
        
Create a receiver from Android Manifest.xml From the Receiver we will get the change in locale intent.
 
public class LocaleChangeReceiver extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {
  Log.i("Locale", "Changed");
 }
}

Monday, May 21, 2012

How to hide media files from a folder Android

Sometimes we need to hide the media files from a folder . In order to attain this create a nomedia file on that folder. Name of the file signaling the media scanner to ignore media in the containing directory and its subdirectories. Developers should use this to avoid application graphics showing up in the Gallery and likewise prevent application sounds and music from showing up in the Music app. The media files only get disappererd only when after reboot of that phone.

file= new File(path);
if (!file.exists()){
   try {
      file.createNewFile();
   } catch (IOException e) {
       e.printStackTrace();
   }
}