"We human being, always find a solution, maybe not today, but if you really want to solve a problem, there’s always a way" – Ma Yun @ Stanford, 2013
Tag Archives: Best Practice

Triggering “checkForUpdate” from android applications

by Md Imran Hasan Hira

Many of the android applications now a days need to fetch data from server. Apps need to be always updated. If one use GCM then all the worries about when to check for update is gone :D. But if this is not case then there remains a question – who will trigger to check for update. There are several design ways to get this trigger in android.

1. Sync Adapter – It is asynchronous. That means we may not get instant result from syncing. Syncing can happen at any time in the background.

2. Alarm Manager – Use Alarm manger as a trigger to check for update. It is also asynchronous.

3. Android Service – We can make our own service which will check for update periodically. In this case it not better than point#2.

above three will work even if the application is not running.

4. Custom thread and Handler – It is also asynchronous. We can have some extra control over the thread while our application is running.

Using Alarm manager will drain battery, this concept is indefinite for android. Cause if we manage your own thread/service that will also cost us the periodic checking. In this case alarm manager helps us by not checking for update, it is just triggering our application at the time when we want to check for update. So the best is to find the correct way for our project.

For Battery Issue and why one should sync adapters these links may be helpful – 
1. http://developer.android.com/…/efficient…/index.html
2. http://developer.android.com/tra…/sync-adapters/index.html


Android common practices

by Md Imran Hasan Hira

Yesterday I was a judge in Mobile App Hackathon, Code Hub 2014. It was really very tough to judge all the work they have done. But I want to mention some common areas. These are definitely not for the experts, but you can have a look at them. Some of the participants missed them and some of them did them really well 🙂 I wrote them in first person.

1. Using Sync Adapter to synchronize the data between your device sqlite database and server.

2. While prototyping your application you can use simple raw/asset file from where you can initialize the database. This will not be needed when you have a web application to get the data from. In that case fetch the data first time from the server and initialize the local sqlite database.

3. It’s better and easier to use the Shared Preference to store small data. Shared Preference is an interface provided by the android OS to store small values in a Key-Value pair style. You can save a boolean value like isAppFirstStart, isDatabaseInitialized,…etc.

4. To fetch data from server most of the people use json format. Use Async Task to retrieve the data.

5. While designing android layout make sure that the buttons/list items are easy enough to tap with our normal finger. Sometime people test the program in one device but use a different device with different screen resolution at the time of presentation. Make them usable for all your device, otherwise stick to a single device.

6. Whenever you are making an application that uses a server, keep a class Server. In that class mock all the function you will be serving from the server. Let’s say it DummyServer. Use this class to all over your project. And whenever the project is working correct with that DummyServer, make another class RealServer which have all the same functionalities but this time it communicates with the server.

7. When using GPS functionality in the device do null check on location ( like LastKnownLocation ) objects you get the from the API. This will save you a lot of time avoiding the crash.

8. *** Save yourself, Never eat up exceptions locally. Try to pass them directly (i.e. re-throwing them) or indirectly (i.e. logging) to upper layer. At the top layer, merge them into readable message to the user.

9. To send any data from server to your phone use Google Cloud Messaging api. Otherwise use an alarm manager according to your need to check for update. To lessen the server load use random time for triggering the update.

10. In projects with lots of views and processing, you can use a common dispatcher which will route your UI events to corresponding controller.

11. There are lots of example code in the internet. It’s not wrong to use them unless there is any licensing problem. Actually it is good ( সোজা বাংলায় চমৎকার হওয়া যাকে বলে ) if you can present some new UI widgets ( like action bar sherlock, sliding components, voice to text input box) in your application. Before using at least make sure that you know about their workflow.

12. Idea changes from time to time. If you have a different approach to a common problem, first mention the regular way, then describe the other better way that you implemented.

13. Use android DDMS to trace the log. Learn how to correctly use the logcat. To learn you can start with writing a bad code which will surely get a null pointer exception, and then run the program, trace the log in logcat.

14. Try to subpackage the codebase. It’ll help to modularize the contents.

15. When there are multiple members working in a single project, use source code version controlling system. If you are not familiar with GIT / SVN, read some online materials about them, they are easy 🙂

 

Thank You for reading, Have a good day!


Theme by Ali Han | Copyright 2025 Md Imran Hasan Hira | Powered by WordPress