Pages

Thursday, November 28, 2013

How to findout unused resources in Android Project

1. Download the AndroidUnusedResources.jar from the following link https://code.google.com/p/android-unused-resources/

2. Once the download is done , copy the AndroidUnusedResources.jar to your project Folder.

3.Open the command prompt and navigate to your project Folder










4. Than type the following command for generating the list of unused resources in your Android Project
java -jar AndroidUnusedResources.jar


Wednesday, November 27, 2013

How to monitor network connectivity using BroadCastReceiver in Android

 1. Now a days most of applications uses INTERNET in their Application. So most of the times we need to know whether the INTERNET is available or not before making any network call..

2. INTERNET Connection might change anytime – and it probably will, given the volatility of a mobile environment.

3. Thankfully Android’s ConnectivityManager allows you to register for a broadcast event that is fired whenever this happens

4. The Source Code for the registering the Broadcast Receiver for finding out the  Network Connectivity is given below

public class Reachability {
    public boolean isReachable = false;
    public boolean isReceiving = false;
   
    private BroadcastReceiver receiver = null;
   
    public static Reachability reachability = null;
    static {
        reachability = new Reachability();
    }
   
    public static boolean registerReachability(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                        .getSystemService(Context.CONNECTIVITY_SERVICE);
       
        if (reachability.isReceiving) {
            context.unregisterReceiver(reachability.receiver);
            reachability.isReceiving = false;
        }
       
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        reachability.isReachable = info != null && info.isAvailable() && info.isConnected();
       
        reachability.receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                ConnectivityManager connectivityManager = (ConnectivityManager) context
                                .getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo info = connectivityManager.getActiveNetworkInfo();
                reachability.isReachable = info != null && info.isAvailable() && info.isConnected();
            }
           
        };
        context.registerReceiver(reachability.receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
        reachability.isReceiving = true;
       
        return reachability.isReachable;
    }
}

5. In the next Step you need to register the BroadCast Receiver for finding out the  Network Connectivity in the Main Application class which extends Application as given below

public class ELSApplication extends Application {
    @Override
    public void onCreate() {

        Reachability.registerReachability(this.getApplicationContext());
      
    }
   
}

6. So whenever your are making the Network call , using the following code  you can check whether the INTERNET is available or not

 if (!Reachability.reachability.isReachable) {
                Toast.makeText(getActivity(), "No Internet", Toast.LENGTH_SHORT).show();
            } else {
                String[] url = { "https://www.dropbox.com" };
                DownloadFileAsync dloadFAsync = new DownloadFileAsync();
                dloadFAsync.execute(url);
            }

7. And the following permissions need to be added in the AndroidManifestFile

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Tuesday, November 26, 2013

How to add ssh key to bitbucket using Windows

1. To generate the ssh key go through the following blog
http://mohan-android.blogspot.in/2013/11/how-to-generate-ssh-keys-using-git-on.html

2. Once the ssh key is generated copy the ssh key and log in to you bitbucket account and select Manage Account and select SSH Keys

3. Select Add key , give the label name in Label section  and paste the sshkey  in the key Section


















4. Once this is done, select Add key.

Screen Recording in Android 4.4

1. In order to perform a screenrecord, you’ll need the Android SDK and a familiarity with adb command.

2. On your Android device running KitKat, enable USB Debugging from Developer Options, which is hidden by default. To enable Developer options, head over to Settings -> About Phone/Tablet and tap on the Build Number seven times, after which you will get a toast notification saying “You’re now a Developer.”
Head over to Settings -> Developer Options now, and then enable the ‘USB Debugging’ option. If you also want your on-screen touches to show in the video, enable the ‘Show touches’ option as well.

3. You are now ready to record a video. Simply connect the Android device to your PC and start a new instance of Command Prompt in Windows or Terminal on Mac. You will now need to navigate to the android sdk folder.

4.  Now, as soon as you enter the command below, the screen recording will start.
adb shell screenrecord /sdcard/filename.mp4
5. You can further customize the speed at which it captures video by using the following command
adb shell screenrecord --bit-rate 8000000 /sdcard/kitkat.mp4

The above command would record at 8Mbps, instead of the default 4Mbps and save it to the SD Card on your device with the name of KitKat. 

6. You can further customize the length of time it records (the default duration is 3-minutes) using the following command
adb shell screenrecord --time-limit 30 /sdcard/kitkat.mp4

The above command would record for 30s, instead of the default 3 Minutes and save it to the SD Card on your device with the name of KitKat. 
7. Other information can be found in the following link

http://developer.android.com/tools/help/adb.html#screenrecord


Thursday, November 21, 2013

How to generate ssh keys using git on windows

1.To generate the ssh key first download the git from from the following link http://code.google.com/p/msysgit/downloads/list

2. Open Git Bash that you just installed (Start->All Programs->Git->Git Bash)



 3. Type in the following : ssh-keygen-t rsa



4. Than it will ask for the file name in which it needs to be saved . Just press Enter.

5.Than it will ask you  for entering passphrase. Just press Enter.



6.Than it will ask you  for reentering passphrase. Just press Enter.


  7. Once the steps mentioned above is done, the ssh key will be generated in the following path C:\Users\--UserName\.ssh Folder





8. The ssh key will be generated in the id_rsa File which is of .pub Format.


Tuesday, November 19, 2013

How to import the Sqlite Database of the Android Application from the Real Android Device / Emulator


1. Most of the times , android developers want to see the values written to sqlite in the Android Device so that they can easily debug for the Errors
2. Following is the format for importing the sqlite from the command line
a. First we have to navigate to the Android-Sdk Folder in the command prompt , than the user should navigate to the platform tools


b.  Than we need to type the following command to fetch the sqlite database
adb -d shell "run-as --packagename cat /data/data/--packagename /databases/--sqlitename > /sdcard//--sqlitename"
where the
i.  --packageName stands for the PackageName specified in the Android Manifest File
ii. --sqlitename stands for the Sqlite Name used in the Application
c. The  following is the sample format
adb -d shell "run-as com.ormlite.contact cat /data/data/com.contact/databases/cont.sqlite > /sdcard/cont.sqlite"


Thursday, November 7, 2013

How to Split a string at every nth position in Java

The Splitting of a String can be achieved by using the regex

Using the following code below  we can split the  string using regex
String[] thiscombo2 = st.split("(?<=\\G..)");

where
i.  The st indicates the String that needs to be split
ii.  The 3 dots after the G indicates every nth position to split. In this case, the 3 dots indicate every 3 positions.

Input : String st = "123124125134135145234235245"
Output :  123 124 125 134 135 145 234 235 245
 
But using the regex for splitting a  String will have certain delay

So to overcome this problem we provide an alternative way

The following code below will split a String at every nth position according to the user's requirements 

  private String[] splitStringEvery(String s, int interval) {
        int arrayLength = (int) Math.ceil(((s.length() / (double) interval)));
        String[] result = new String[arrayLength];
        int j = 0;
        int lastIndex = result.length - 1;
        for (int i = 0; i < lastIndex; i++) {
            result[i] = s.substring(j, j + interval);
            j += interval;
        } // Add the last bit
        result[lastIndex] = s.substring(j);
        return result;
    }


Performance Tests using the regex split and splitStringEvery()

200 characters long string - interval is 3

Using split(" (<=\\G.{"+count+"})") performance (in Milliseconds):

3,1,1,1,0,1,1,2,0,0

Using splitStringEvery() ( substring () ) performance (in Milliseconds):

1,0,0,0,0,0,0,0,0,0