Friday, December 11, 2015

keepclasseswithmembernames and keepclasseswithmembers difference

Some of the important attributes in proguard project properties include

-keep attribute
can be used with class, inteface enum etc

eg: -keep class com.smartandroidians.views.FloatingActionButtonBehavior which is not used in any java class and only used in xml files just like a string as


proguard remove this class because it feels like it is not used anywhere but it is a very important class  for the app rather floating button to function.

1) You can do something like this,
-keep class com.smartandroidians.views.FloatingActionButtonBehavior { *; }

or rather better approach is

2) -keepclasseswithmembernames class * {
    public (android.content.Context, android.util.AttributeSet);

How to get the package name of default sms app

Code snippet for getting the default package name of SMS

for below kitkat

String defApp = Settings.Secure.getString(getContentResolver(), "sms_default_application");
PackageManager pm = getApplicationContext().getPackageManager();
Intent iIntent = pm.getLaunchIntentForPackage(defApp);
ResolveInfo mInfo = pm.resolveActivity(iIntent,0);
Log.i(TAG, "apk:"+mInfo.activityInfo.packageName);
above or equal to kitkat


SQLite no concept of boolean - stored as 0 or 1

A simple but useful

SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true).

select * from table where deleted='false' which is wrong instead you have to write 
select * from table where deleted=0

Saturday, March 21, 2015

How to use callback or interface - Pass data from Asynctask to activity

To get data from a separate thread to the the main UI thread which will be in different stand alone class can get the result through callbacks. Consider AsyncTask in a separate stand alone class and we need to get data from AsyncTask to the Activity, we can get it through callbacks. The steps are as,

1. Define an interface in AsyncTask
2. Pass the object of interface when calling AsyncTask from Activity
3. In AsyncTask onPostExecute() pass the result to activity

class MyTask extends AsyncTask {
public interface OnUpdateListener {
public void onUpdate(MyObject obj);
OnUpdateListener listener;
MyTask() {
public void setUpdateListener(OnUpdateListener listener) {
this.listener = listener;
MyObject doInBackground() {
return obj;
onPostExecute(MyObject obj) {
if (listener != null) {


In Activity, we can get the object by

MyActivity extends Activity {
void onCreate (Bundle savedInstanceState) {
MyTask  task = new MyTask();
task. setUpdateListener(new MyTask. OnUpdateListener() {
public void onUpdate(MyObject obj) {


Tuesday, February 3, 2015

Fetch all Contacts from Contacts Content Provider in Android

Here I am going to explain how to fetch all contacts from Contacts content provider. As you already know content provider is used to share data between applications. Here we will get the data from contacts content provider to our application. First we have to create an object of ContentResolver.

 ContentResolver contentResolver = getContentResolver()  

Then query the content resolver using the Contacts URI.

 Cursor contactsCursor = resolver.query(Contacts.CONTENT_URI, null, null, null, null);  

After querying we will get the contacts ID. Using contacts ID, we can get the phone number,

 String id = contactsCursor.getString(contactsCursor.getColumnIndex(Contacts._ID));  
 // to get phone number of respective contact  
 Cursor phoneCursor = resolver.query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = ?", new String[] { id }, null);  
 String phoneNumber = "";  
 if (phoneCursor.moveToFirst()) {  
      phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(Phone.NUMBER));  

Then to get the contacts name, contacts photo and email, use the below code

 String name = contactsCursor.getString(contactsCursor.getColumnIndex(Contacts.DISPLAY_NAME));  
 String photoUri = "";  
 photoUri = contactsCursor.getString(contactsCursor.getColumnIndex(Photo.PHOTO_URI));  
 // to get email id of respective contact  
 Cursor emailCursor = resolver.query(Email.CONTENT_URI, null, Email.CONTACT_ID + " = ?", new String[] { id }, null);  
 String email = "";  
 if (emailCursor.moveToFirst()) {  
      email = emailCursor.getString(emailCursor.getColumnIndex(Email.ADDRESS));  

Here some tips for SQLITE query. To limit the records, in query() API for the last parameter first sort for an attribute, then limit the records using limit ,

Eg: Contacts._ID DESC LIMIT 10,10

Here contacts will be sorted in descending order by _ID and we will get 10 records from 10 to 20.

The new query looks like this,

 Cursor contactsCursor = resolver.query(Contacts.CONTENT_URI, null, null, null, Contacts._ID+" DESC LIMIT 10,10");  

We can get the full source code by clcicking HERE

The screenshots are here as follows,

Also by clicking on the selected contact, it will call to that particular contact, if it has a phone number.

Monday, February 2, 2015

Driving direction between two locations in Android

Here, I am explaining the driving direction between two locations in google map. It is an utility function. Here we are passing two locations to a method called "showMapFromLocation", first convert the location to corresponding latitude and longitude. Then using launch google map using intent. Here I just hard coded the package and launcher activity of google map. It is better to check the application (google map) is already there in the device using packagemanager.

The utility function is as follows,

 private void showMapFromLocation(String src, String dest) {  
           double srcLat = 0, srcLng = 0, destLat = 0, destLng = 0;  
           Geocoder geocoder = new Geocoder(this, Locale.getDefault());  
           try {  
                if (isOnline()) {  
                     List<Address> srcAddresses = geocoder.getFromLocationName(src,  
                     if (srcAddresses.size() > 0) {  
                          Address location = srcAddresses.get(0);  
                          srcLat = location.getLatitude();  
                          srcLng = location.getLongitude();  
                     List<Address> destAddresses = geocoder.getFromLocationName(  
                               dest, 1);  
                     if (destAddresses.size() > 0) {  
                          Address location = destAddresses.get(0);  
                          destLat = location.getLatitude();  
                          destLng = location.getLongitude();  
                     String desLocation = "&daddr=" + Double.toString(destLat) + ","  
                               + Double.toString(destLng);  
                     String currLocation = "saddr=" + Double.toString(srcLat) + ","  
                               + Double.toString(srcLng);  
                     // "d" means driving car, "w" means walking "r" means by bus  
                     Intent intent = new Intent(android.content.Intent.ACTION_VIEW,  
                               Uri.parse("" + currLocation  
                                         + desLocation + "&dirflg=d"));  
                               & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);  
           } catch (IOException e) {  
                Log.e(TAG, "Error when showing google map directions, E: " + e);  
           } catch (Exception e) {  
                Log.e(TAG, "Error when showing google map directions, E: " + e);  

We can select which mode of driving direction we need according to the flag "dirflg".
    - dirflg = d, means driving by car
    - dirflg = w, means by walking and
    - dirflg = r, means by bus

The screenshots are like,

One last thing is we have to add google play services library to show the google maps.

Tuesday, January 27, 2015

Gesture detection and scrollview issue

In one of my latest application when I implemented ViewFlipper with gesture listener, I come across a strange issue. All child of ViewFlipper are scrollViews. I implemented simple gesture listener like when swipe to left or right will take to the next scrollView. But the issue was swipe functionality is not working in scrollView. If the ViewFlipper child are any other layout like Linearlayout, swipe functionality is working fine. As usual, I debugged and found that, we have to override the dispatchTouchEvent(MotionEvent e) of activity class.

The dispatchTouchEvent() will look like this,
public boolean dispatchTouchEvent(MotionEvent ev) {
return gestureDetector.onTouchEvent(ev);
Where gestureDetector is an instance of android.view.GestureDetector class.


Thursday, January 22, 2015

How to avoid MEMORY LEAKS in Android

Memory leaks means resources which are not available for garbage collection. GC will free resources which are not referenced. Sometimes a single reference can prevent a large set of objects from being garbage collected. For example, some static variables which are referenced in an activity may not be for GC, because as long as the reference exists, the Activity will be kept in memory, leaking all of its views.

Code snippet from Romain Guy is as,

private static Drawable sBackground;

protected void onCreate(Bundle state) {

  TextView label = new TextView(this);
  label.setText("Leaks are bad");

  if (sBackground == null) {
    sBackground = getDrawable(R.drawable.large_bitmap);

Here, the static drawable sBackground will leak.

The solution here is when the activity gets destroyed, release the drawable in destroy and unbind all callbacks

Here, our onDestroy() will look like this,

protected void onDestroy() { 
sBackground = null;

So that the drawable sBackground will be available for GC and can prevent memory leak when activity gets destroyed.

Another common cause for memory leak is non-static inner classes in an Activity. This commonly comes when creating fragments in an Activity. Either create static inner classes or create stand alone classes.