Posts

Showing posts from 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

app:layout_behavior="com.smartandroidians.views.FloatingActionButtonBehavior"

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.

Solution.
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

Telephony.Sms.getDefaultSmsPackage(this)

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).

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

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

classMyTaskextendsAsyncTask{publicinterfaceOnUpdateListener{publicvoid onUpdate(MyObject obj);}OnUpdateListener listener;MyTask(){}publicvoid setUpdateListener(OnUpdateListener listener){this.listener = listener;}MyObject doInBackground(){return obj;} onPostExecute(MyObject obj){if(listener !=null){ listener.onUpdate(obj);}}
}

In Activity, we can get the object by

MyActivityextendsActivity{void onCreate (Bundle savedInstanceState){super.onCreate(savedInstanceState);MyTask task =newMyTask(); task. setUpdateListener(newMyTask.On…

Fetch all Contacts from Contacts Content Provider in Android

Image
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(P…

Driving direction between two locations in Android

Image
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, 1); if (srcAddresses.size() > 0) { Address …

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) { super.dispatchTouchEvent(ev); return gestureDetector.onTouchEvent(ev); } Where gestureDetector is an instance of android.view.GestureDetector class.

Reference:
http://stackoverflow.com/questions/8330187/gesture-detection-and-scrollview-issue

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,

privatestaticDrawable sBackground;@Overrideprotectedvoid onCreate(Bundle state){super.onCreate(state);TextView label =newTextView(this); label.setText("Leaks are bad");if(sBackground ==null){ sBackground = getDrawable(R.drawable.large_bitmap);} label.setBackgroundDrawable(sBackground); setContentView(label);} 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…