Tuesday, 27 October 2015

What is the relationship between the life cycle of an AsyncTask and an Activity? What problems can this result in? How can these problems be avoided?

An AsyncTask is not tied to the life cycle of the Activity that contains it. So, for example, if you start an AsyncTask inside an Activity and the user rotates the device, the Activity will be destroyed (and a new Activity instance will be created) but the AsyncTask will not die but instead goes on living until it completes.

Then, when the AsyncTask does complete, rather than updating the UI of the new Activity, it updates the former instance of the Activity (i.e., the one in which it was created but that is not displayed anymore!). This can lead to an Exception (of the type java.lang.IllegalArgumentException: View not attached to window manager if you use, for instance, findViewById to retrieve a view inside the Activity).

There’s also the potential for this to result in a memory leak since the AsyncTask maintains a reference to the Activty, which prevents the Activity from being garbage collected as long as the AsyncTask remains alive.

For these reasons, using AsyncTasks for long-running background tasks is generally a bad idea . Rather, for long-running background tasks, a different mechanism (such as a service) should be employed.

---------------------------------------------------Answer -------------------------------------------------------

AsyncTask is an abstract Android class which helps the Android applications to handle the Main UI thread in efficient way. AsyncTask class allows us to perform long lasting tasks/background operations and show the result on the UI thread without affecting the main thread.

1. AsyncTask processes are not automatically killed by the OS. AsyncTask processes run in the background and is responsible for finishing it's own job in any case. You can cancel your AsycnTask by calling cancel(true) method.

=>> This will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(Object) method is called instead of onPostExecute() after doInBackground() returns.

2. After completion of it's operation, the background thread it's working on is stopped. AsyncTask has an onPostExecute() which is called once your work is finished. This method is called after doInBackground() method completes processing. Result from doInBackground() is passed to this method.


Finally before calling to onPostExecute() method after doInBackground() method .Call  isCancelled() method to avoid exception at run time.

Monday, 26 October 2015

Under what condition could the code sample below crash your application?

 How would you modify the code to avoid this potential problem?

    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);
    sendIntent.setType(HTTP.PLAIN_TEXT_TYPE); // "text/plain" MIME type
    startActivity(sendIntent);

An implicit intent specifies an action that can invoke any app on the device able to perform the action. Using an implicit intent is useful when your app cannot perform the action, but other apps probably can. If there is more than one application registered that can handle this request, the user will be prompted to select which one to use.
However, it is possible that there are no applications that can handle your intent. In this case, your application will crash when you invoke startActivity(). To avoid this, before calling startActivity() you should first verify that there is at least one application registered in the system that can handle the intent. To do this use resolveActivity() on your intent object:
    // Verify that there are applications registered to handle this intent
    // (resolveActivity returns null if none are registered)
    if (sendIntent.resolveActivity(getPackageManager()) != null) {
        startActivity(sendIntent);
    } 


Wednesday, 14 October 2015

Support icons and images into different device screen size and resolution

First create xxxhdpi image size according below mention , then generate 9 patch images from  
https://romannurik.github.io/AndroidAssetStudio/nine-patches.html

After that use generated image in your android project- That's easy your effort and save time

1. Action bar , tab and dialog icons --> 96x96 (xxxhdpi) 
2. Normal images size -->  192x192 (xxxhdpi)
3. App logo --> 192 × 192 (xxxhdpi)

                           -->  512 × 512 (Google Play store)

When you want to close and exit from android application.

Put android minimum version - 16
then use-

finishAffinity();

Tuesday, 6 October 2015

Type of android intents and dialog's

Intent: is a message passing mechanism between components of android except ContentProvider .You can use intent to start any component.
Sticky Intent : It sticks with android for future broad cast listeners. Ex-if BATTERY_LOW event occures then that intent will be stick with android so that if in future battery low occures while app is running that intent will be fired.
Pending Intent: If you want someone to perform any Intent operation at future point of time on behalf of you ,then we will use pending Intent.
Ex- 
Booking a ticket at late night when your application is not running. In this scenario we will create a pending intent to start a service which can book tickets at late night and hand it over to Alarm Manager to fire it at that time.

Types of DialogBox

A dialogBox is a small window that prompts the user to make a decision or enter additional information. A dialogBox does not fill the screen and is normally used for modal events that require users to take an action before they can proceed, like to select date from DatePicker Dialog, to select time from time picker Dialog, to show the Progress, to show a list of items or options to select etc.
Alert Dialog:
     A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.
DatePicker Dialog and TimePicker Dialog: 
       A dialog with a pre-defined UI that allows the user to select a date or time.
Progress Dialog: 
     A Dialog that can show a title, ProressBar and a message, or a custom layout.
List Dialog: 
     A Dialog that can show a title, List of items and a message, or as above a custom layout.

How to get android application crashing into your email id


package com.sanjay.android;
import org.acra.ACRA;
import org.acra.ReportField;
import org.acra.ReportingInteractionMode;
import org.acra.annotation.ReportsCrashes;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Logger;
import com.google.android.gms.analytics.Tracker;

import android.app.Application;
import android.content.Context;

@ReportsCrashes(mailTo = "sanjay01feb@gmail.com",
customReportContent = { ReportField.APP_VERSION_CODE, ReportField.APP_VERSION_NAME, ReportField.ANDROID_VERSION, ReportField.PHONE_MODEL, ReportField.CUSTOM_DATA, ReportField.STACK_TRACE, ReportField.LOGCAT },                
mode = ReportingInteractionMode.TOAST,
resToastText =android.R.string.VideoView_error_text_unknown )

public class MyApplication extends  Application {

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
ACRA.init(this);
}
public synchronized  Tracker getTracker() {
        GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
        analytics.getLogger().setLogLevel(Logger.LogLevel.VERBOSE);
       // analytics.setLocalDispatchPeriod(15);
        Tracker t = analytics.newTracker(getResources().getString(R.string.GA_TRACKER_ID));
//        t.enableAdvertisingIdCollection(true);
    return t;
}
}


Note - download acra-4.6.1.jar and add as library in your android project .






android / platform / development / samples