Search This Blog

Saturday 25 May 2013

Android phenomenal success story : Android popular because its good not because its cheap

Android success is phenomenal and incredible. Its start a new debate on reason of being most used mobile operating system. First thing in our mind comes, except intense market competition, How android make its ground ! Android success does not come in day or not for few days. Its going to continue the market leader in coming decade

Flexibility and competitiveness 

Android is damn flexible due to its open source nature. Android run on a mobile of price $100 to $1000. Google giant allow manufacturer to customize it according to their requirement and target users. Many manufacturer taste a success with android. Either its world smartphone leader Samsung or developing nation India's Micro-max, Android help everyone. Android is competitive and adapt changes very fast. It launches lots version in less than a decade and continue to do so.

A huge application store Google Play

Android is much younger than iOS.  still it has big market compare to iPhone. Android users have better choice in application. Most importantly android market contains lots of free application compare to Apple market. It makes users to feel better as they had options. Its free application are not crap, its because it take minimum effort and money to make android application.

A large developer network

Android developer network is big because of Google good decision to choose Java as their development platform. Even-though Developer can make their application in native language like C,C++ using NDK in android. Java is consider to be a strong and popular language. It take minimum effort for Java developer to make android applications.

Myth 

I had read some post who are discussing that Android is popular because its cheap. But i strongly disagree with point. Its proven that people does not tend to cheap product until its not prove to be as good as others.
People will doubt Dominoes if they start selling their pizza for few cents.Today many android phone are best selling smartphone which are not cheap. Let Samsung S3 and S4 , they are come into category of cheap. Android is popular because its strong, User friendly and flexible


how to debug a running application in android?

Generally, we can launch an android application in two mode 1) Running and 2) Debugging. While running an application, it works fast, faster speed allow developer to navigate android application smoothly. But while launching in debug mode, its comparatively slow. So i was searching to find a way to launch application in run mode and whenever i want, switching to debug mode. Finally i did it. so lets see

It contains only two steps

1) Launch your application in run mode , and do whatever you want to do




Step 2) Open the DDMS tool, select your device and then select your application package name of application for which you want to change the mode. And click on debug. See Screen shot



Saturday 18 May 2013

Android Screen Rotation : handling Activity Runtime orientation change

Handling orientation change in android is bit interesting topic to explore. on change of orientation your Android device's width changes to height and vice versa. It make developer to force some changes in layout for better user expeperience. We can handle user interface while changing orientation using two way. Lets discuss best possible way to handle activity orientation change.

1) Avoid recreating activity using android:configChanges="orientation|screenSize" and android:screenOrientation="user". Its not easy to save state all the process running and resuming them. It may force you write lots of code. If Application structure is complex and User Interface is similar in all orientation then using this way is prodigy. Even thought this way give you event of changing orientation in onConfigurationChanged(Configuration newConfig), but it add overhead to change layouts.

Tip1 : try to minimize the padding and margin to minimize the impact of screen size.
Tip 2: Use Relative Layout

Pros : Avoiding huge among of code if process are in enough numberCons : Using this way in multipane layout is not possible. Its shows the bad user experience.


2) Handle recreating activity and assign fresh resource on base of orientation. Attribute android:screenOrientation="user" force activity to recreating to load the new resource on the base of current screen orientation. Remove android:configChanges="orientation|screenSize" in this case
Tip 1: Create two layout folder inside resource layout for portrait and layout-land for landscape
Tip 2: Android provide a way to save current activity state while changing application orientation. Save your data inside onSaveInstanceState(Bundle outState);


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt("progress", 10);
        outState.putBoolean("isRecreated", true);
        super.onSaveInstanceState(outState);
    }

Tip 3: Now check in onCreate(Bundle savedInstanceState), is it creating or recreating. Restore the process and thread in case recreating

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int progress = 0;
        boolean isRecreated = false;
        if (savedInstanceState != null) {
            // Restore Process and Thread
            progress = savedInstanceState.getInt("progress");
            isRecreated = savedInstanceState.getBoolean("isRecreated");
        } else {
            // Start Fresh Process and Thread
        }

        Log.e(String.valueOf(progress), String.valueOf(isRecreated));
    }

Pros : Code Overhead
Cons : Better user exeperience. easy to handle Multipane layout

Sunday 12 May 2013

Android Download Manager Example , Controlling download in android application

Android provide download manager to download different kind of data. Download Manager make developer life much easier. Handling download becomes very easy.
DownloadManager class support only after API 9

Implementing DownloadManager is easy. Lets have a look how we can start downloading using DownloadManager  in android application

    /**
     * Start Download
     */
    public void startDownload() {
        DownloadManager mManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        Request mRqRequest = new Request(
                Uri.parse("http://androidtrainningcenter.blogspot.in/2012/11/android-webview-loading-custom-html-and.html"));
        mRqRequest.setDescription("This is Test File");
//        mRqRequest.setDestinationUri(Uri.parse("give your local path"));
        long idDownLoad=mManager.enqueue(mRqRequest);
    }



Request mRqRequest = new Request(
Uri.parse("http://androidtrainningcenter.blogspot.in/2012/11/android-webview-loading-custom-html-and.html")); allow you request from DownloadManager to start downloading. You can set your custom Description using Request class.

Handling Download : Stopping, Removing and opening download file

When you start download request, it return you one id. Application can handle download using that id

long idDownLoad=mManager.enqueue(mRqRequest);
mManager.remove(idDownLoad);
try {
mManager.openDownloadedFile(idDownLoad);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

Catching Action Using BroadCastReceiver And Notify User

Take on BroadCastReceiver and Register it. You will ACTION_DOWNLOAD_COMPLETE if application register this Intent-Filter.

    DownLoadComplte mDownload;

    @Override
    protected void onStart() {
        super.onStart();
        mDownload = new DownLoadComplte();
        registerReceiver(mDownload, new IntentFilter(
                DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mDownload);
    }

    private class DownLoadComplte extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equalsIgnoreCase(
                    DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
                Toast.makeText(context, "Download Complte", Toast.LENGTH_LONG)
                        .show();
            }
        }
    }

Viewing your downloaded File


public void viewDownload() {
Intent mView = new Intent();
mView.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
startActivity(mView);
}

Saturday 4 May 2013

How to make Android service unstoppable and run it continuously

Android service runs in different mode with help of flag e.g START_CONTINUATION_MASK, START_NOT_STICKY. It enable to control the behavior of Android service life. But service will work until user allow it work. If he/she stops it, it will destroy automatically. More than often, we required it to run unstoppable  If in case user stop, re-instance the old service instance.
For some people, it may malware but sometimes we have to full filled client requirement and that's all our purpose is.

Case when your service can be stopped



  • User stop it forcefully
  • Low Memory situation
  • Device restarted


Handling first two case


When ever we stop service forcefully (or os kill it), it will call onDestory() method. First concept is to use one receiver and send one broadcast whenever service destroy. And restarted service again.

Third case if device is restarted then already we had onBootCompleted action for receiver to catch

Lets go steps by Step to make our Android Service unstoppable

Step 1) Create one Android BroadCastReciever and register it for two action


Manifest

        <service android:name="ServiceTest" >
        </service>

        <receiver android:name="ReceiverCall" >
            <intent-filter>
                <action android:name="com.android.techtrainner" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

com.android.techtrainner is the custom action. BroadCastReceiver Class contain code to restart service again

public class ReceiverCall extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("Service Stops", "Ohhhhhhh");
        context.startService(new Intent(context, ServiceTest.class));;
    }

}


Step 2) Create Android service class , do some task there (I have taken one Timer to print Log) and in onDestory()


    public void onDestroy() {
        try {
            mTimer.cancel();
            timerTask.cancel();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Intent intent = new Intent("com.android.techtrainner");
        intent.putExtra("yourvalue", "torestore");
        sendBroadcast(intent);
    }

Run your application and go to setting, See running service, you will TestService. Try to close it , ohhhhhh it will not close.

DownloadSource Application



Android News and source code